简体   繁体   English

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

[英]Swagger not detecting Api built with Spring Data Rest

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!我正在使用 swagger 为我的 API 生成文档的 Spring Boot 应用程序,我正在使用 Spring data rest 生成 Api,但是当我运行该应用程序时,我收到了 swagger 消息:规范中没有定义任何操作!

This my Api code :这是我的 Api 代码:

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

}

And this my configuration file :这是我的配置文件:

@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>

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我问的是 Swagger 是否能够为 Spring data rest 生成的 Api 生成文档,或者我应该使用带有注释的 @RestController @Api,@ApiOperation

I'm using spring boot version : 2.1.3.RELEASE我使用的是 Spring Boot 版本:2.1.3.RELEASE

This did the trick for me, upgrading to the latest snapshot of springfox这对我有用,升级到 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>

Then use @EnableSwagger2WebMvc instead of @EnableSwagger2 :然后使用@EnableSwagger2WebMvc而不是@EnableSwagger2

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

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

}

All thanks to Yann39 .这一切都归功于Yann39

Based on the discussions on this springfox Github issue looks like you need to amend your config class to include an additional annotation, ie:根据关于springfox Github 问题的讨论,您似乎需要修改配置类以包含附加注释,即:

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

I can't test it as I only use swagger for Rest controllers at present but hope this helps.我无法测试它,因为我目前只对 Rest 控制器使用 swagger,但希望这会有所帮助。

Did you get to the bottom of this using the latest version of Springfox 3.0.0-SNAPSHOT? 您是否使用最新版本的Springfox 3.0.0-SNAPSHOT深入了解此问题? I can't see the Spring Data Rest endpoints that are generated in Swagger UI using the latest versions of both Springfox and Spring Boot 2. 我看不到使用最新版本的Springfox和Spring Boot 2在Swagger UI中生成的Spring Data Rest端点。

pom.xml pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <swagger>3.0.0-SNAPSHOT</swagger>
</properties>

<dependencies>
<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger}</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger}</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>${swagger}</version>
    </dependency>

</dependencies>

application.yaml应用程序.yaml

spring:
    data:
        rest: 
            base-path: /v1

java config配置文件

@Configuration
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class SwaggerConfig 

尝试RequestHandlerSelectors.any()而不是RequestHandlerSelectors.basePackage("")

Verify your package & add验证您的包裹并添加

@Configuration
@EnableSwagger2

to the spring boot configuration file到spring boot配置文件

   @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("your.package.name")).paths(PathSelectors.any()).build()
                .apiInfo(apiEndPointInfo());
    }

only those dependencies are required只需要那些依赖项

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

your final config file should looks like this你的最终配置文件应该是这样的

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@Configuration
@EnableSwagger2
public class TestappApplication implements WebMvcConfigurer {

    private static final String PREFIX = "/WEB-INF/views/";
    private static final String SUFFIX = ".jsp";

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

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp(PREFIX, SUFFIX);

    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("your.package.scan.to.swagger.annotations")).paths(PathSelectors.any()).build()
                .apiInfo(apiEndPointInfo());
    }

    public ApiInfo apiEndPointInfo() {
        return new ApiInfoBuilder().title("Spring Boot Rest API").description("user Management API")
                .contact(new Contact("zouhair kasmi", "website.com/", "myemail@gmail.com"))
                .license("Apache 2.0").licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .version("0.0.1-SNAPSHOT").build();

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM