Swagger没有检测到使用Spring Data Rest构建的Api

问题描述 投票:1回答:2

我正在使用swagger为我的API生成文档的Spring启动应用程序,我使用Spring数据休息生成Api但是当我运行应用程序时,我得到了一个招摇信息:没有在规范中定义的操作!

这是我的Api代码:

@Api(tags = "projets")
@RepositoryRestResource(collectionResourceRel = "projets", path = "projets")
public interface IProjectRepository extends JpaRepository<Project, Long> {

}

这是我的配置文件:

@Configuration
@EnableSwagger2
public class QfactoryConfiguration {

    @Bean
     public Docket getDocketInstance() {
         return new Docket(DocumentationType.SWAGGER_2) 
                 .apiInfo(new ApiInfoBuilder()
                            .title("Spring Boot project")
                            .description("Spring Boot bootstrap project")
                            .version("0.1")
                            .license("Unlicense")
                            .build())
                  .select()  
                  .apis(RequestHandlerSelectors.basePackage("com.errabi.qfactory.repositories"))             
                  .paths(PathSelectors.any())                          
                  .build();  
     }




}

这就是我正在使用的依赖项:

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

我问的是,Swagger是否能够为Spring数据生成的Api生成文档,或者我应该使用注释为@ Api的@RestController,@ ApiOperation

我正在使用spring boot版本:2.1.3.RELEASE

java spring-boot swagger spring-data-rest springfox
2个回答
1
投票

基于对this springfox Github问题的讨论看起来你需要修改你的配置类以包含一个额外的注释,即:

@Configuration
@EnableSwagger2
@Import({springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class})

我无法测试它,因为我目前只使用Swagger for Rest控制器,但希望这会有所帮助。


0
投票

这对我有用,升级到springfox的最新快照

<dependencies>
...
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-data-rest</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>
...
</dependencies>

<repositories>
    <repository>
        <id>JFrog</id>
        <name>JFrog Snapshot Repository</name>
        <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
    </repository>
</repositories>

然后使用@EnableSwagger2WebMvc而不是@EnableSwagger2

@SpringBootApplication
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

一切都归功于Yann39

© www.soinside.com 2019 - 2024. All rights reserved.