Swagger not detecting Api built with Spring Data Rest

后端 未结 5 1886
我在风中等你
我在风中等你 2021-01-19 18:10

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

相关标签:
5条回答
  • 2021-01-19 18:37

    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>
    

    Then use @EnableSwagger2WebMvc instead of @EnableSwagger2:

    @SpringBootApplication
    @EnableSwagger2WebMvc
    @Import(SpringDataRestConfiguration.class)
    public class MyApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    
    }
    

    All thanks to Yann39.

    0 讨论(0)
  • 2021-01-19 18:37

    Based on the discussions on this springfox Github issue looks like you need to amend your config class to include an additional annotation, i.e.:

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

    0 讨论(0)
  • 2021-01-19 18:38

    Verify your package & add

    @Configuration
    @EnableSwagger2
    

    to the spring boot configuration file

       @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();
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-19 18:40

    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

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

    java config

    @Configuration
    @EnableSwagger2WebMvc
    @Import(SpringDataRestConfiguration.class)
    public class SwaggerConfig 
    
    0 讨论(0)
  • 2021-01-19 18:53

    Try RequestHandlerSelectors.any() instead of RequestHandlerSelectors.basePackage("").

    0 讨论(0)
提交回复
热议问题