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

我正在开发一个使用 swagger 为我的 API 生成文档的 Spring Boot 应用程序,我正在使用 Spring data rest 来生成 Api,但是当我运行该应用程序时,我收到了 swagger 消息:没有定义操作符合规范!

I'm working on a spring boot application using swagger to generate docs for my API ,I'm using Spring data rest to generate the Api but when I run the app I get the swagger message : No operations defined in spec!

这是我的 Api 代码:

This my Api code :

@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();  
     }




}

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

And this the dependencies that I'm using :

<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 data rest 生成的 Api 生成文档,或者我应该使用带有注释的 @RestController @Api,@ApiOperation

I'm asking is the Swagger is able to generate docs for Api generated by Spring data rest or I should use a @RestController with annotations @Api,@ApiOperation

我使用的是 Spring Boot 版本:2.1.3.RELEASE

I'm using spring boot version : 2.1.3.RELEASE

推荐答案

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

This did the trick for me, upgrading to the latest snapshot of 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.

相关文章