Spring Boot - Spring Data REST从存储库生成swagger文档

时间:2018-03-05 19:11:59

标签: maven spring-boot swagger spring-data-rest swagger-maven-plugin

我在使用Spring Data Rest的Spring Boot中有一个应用程序,我正在尝试使用swagger-maven-plugin生成Swagger的文档。生成Controller文档没有问题,但存储库没有。

我在pom.xml中配置了以下格式的swagger-maven-plugin:

                <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>abelendo@email.com</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>

我的CarRepository:

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

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

}

是否可以使用swagger-maven-plugin生成存储库文档?

1 个答案:

答案 0 :(得分:0)

为了生成@RepositoryRestResource的详尽文档,您需要做的是:

  1. 在您的项目中拉io.springfox:springfox-data-rest依赖项
  2. springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration类导入您的灵活配置类。
@Configuration
@EnableSwagger2
@Import(SpringDataRestConfiguration.class)
public class SwaggerConfig {

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

}

编辑:swagger config类不适用于spring-boot 2.0,spring-data 2.0和spring-data-rest 3.0:swagger存在一个未解决的问题(与swagger无关) Java8,而spring-boot和spring-data是)。有关详情,请参见此处:https://github.com/springfox/springfox/issues/2298

但是,我设法通过修补一些招摇类来解决它。该补丁背后的想法是使用Java Optional而不是Guava和Java反射。修补的类为:

  • 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