Gonzalo Santiago
Gonzalo Santiago

Reputation: 348

Spring Boot - Spring Data REST generate swagger documentation from repository

I have an application in Spring Boot with Spring Data Rest and I am trying to generate the documentation with Swagger using swagger-maven-plugin. The Controller documentation is generated without problems but the repository does not.

I have configured the swagger-maven-plugin of the following form in my pom.xml:

                <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>3.1.6</version>
                <configuration>
                    <apiSources>
                      <apiSource>
                            <springmvc>true</springmvc>
                            <locations>
                                <location>com.abelendo.repository</location>
                                <location>com.abelendo.controller</location>
                            </locations>
                            <schemes>http</schemes>
                            <host>localhost:8080</host>
                            <basePath>/</basePath>
                            <info>
                                <title>Swagger Maven Plugin Spring Boot for cars</title>
                                <version>v1</version>
                                <description>Working sample of Spring Boot for cars annotations</description>
                                <termsOfService>
                                    http://www.github.com
                                </termsOfService>
                                <contact>
                                    <email>[email protected]</email>
                                    <name>Abelendo Cars</name>
                                    <url>http</url>
                                </contact>
                                <license>
                                    <url>http://www.license.com</url>
                                    <name>License name</name>
                                </license>
                            </info>
                            <!-- Support classpath or file absolute path here.
                            1) classpath e.g: "classpath:/markdown.hbs", "classpath:/templates/hello.html"
                            2) file e.g: "${basedir}/src/main/resources/markdown.hbs",
                                "${basedir}/src/main/resources/template/hello.html" -->
                            <templatePath>${basedir}/templates/strapdown.html.hbs</templatePath>
                            <outputPath>${basedir}/generated/document.html</outputPath>
                            <outputFormats>yaml</outputFormats>
                            <swaggerApiReader>com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader</swaggerApiReader>
                            <swaggerDirectory>generated/swagger-ui</swaggerDirectory>
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

My CarRepository:

@Api(tags = "CarsRepo")
@RepositoryRestResource(path = "cars")
public interface CarRepository extends CrudRepository<Car, Long> {

<S extends Car> S save(@Valid S cars);

}

Is it possible to generate the repository documentation with swagger-maven-plugin?

Upvotes: 1

Views: 1250

Answers (2)

MERLIN S
MERLIN S

Reputation: 51

I just 1 dependency in pom.xml

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-data-rest -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>3.0.0</version>
    </dependency>

Then import @Import(SpringDataRestConfiguration.class) in Respository

and it generates documentation

Upvotes: 0

Gaetan
Gaetan

Reputation: 2826

In order to generate swagger documentation for a @RepositoryRestResource, all you need to do is:

  1. Pull the io.springfox:springfox-data-rest dependency in your project
  2. Import the springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration class in your swagger configuration class.
@Configuration
@EnableSwagger2
@Import(SpringDataRestConfiguration.class)
public class SwaggerConfig {

  @Bean
  public Docket api() { ... }

}

Edit: the swagger config class doesn't work with spring-boot 2.0, spring-data 2.0 and spring-data-rest 3.0: there's an open issue in swagger (linked to swagger not being Java8 while spring-boot and spring-data are). See here for details: https://github.com/springfox/springfox/issues/2298.

However, I managed to solve it by patching a few swagger classes. The idea behind the patch is to use Java Optional instead of Guava and Java reflection. The patched classes are:

  • springfox.documentation.spring.data.rest.EntityServicesProvider
  • springfox.documentation.spring.data.rest.EntityContext
  • springfox.documentation.spring.data.rest.EntityDeleteExtractor
  • springfox.documentation.spring.data.rest.EntityFindAllExtractor
  • springfox.documentation.spring.data.rest.EntityFindOneExtractor
  • springfox.documentation.spring.data.rest.EntitySaveExtractor

Upvotes: 0

Related Questions