Added Springfox Swagger-UI and it's not working, what am I missing?

后端 未结 11 1493
甜味超标
甜味超标 2020-12-02 00:00

Following the instructions here:

http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

I added these dependencies to my project:



        
相关标签:
11条回答
  • 2020-12-02 00:25

    Note: springfox Version 3.0.0 throw Whitelabel Error Page with below solution

    Spring boot 2.3.3.RELEASE

    Even with this it doen't matter if request mapping having path variable.

    After going through many solution page nothing worked. but

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

    Run (Definitely needed) : Mvn clean

    @Configuration
    @EnableSwagger2
    

    No need to add new Docket Bean for simple swagger configuration(i.e. path and basepackage)

    0 讨论(0)
  • 2020-12-02 00:28

    If you are using Spring Boot Version >= 2.2, I recommend using SpringFox Swagger version 3.0.0. Keep your pom.xml dependency configuration like this:

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
    </dependency>
    

    Keep your Swagger configuration class like below:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    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;
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
    public static final Contact DEFAULT_CONTACT = new Contact(
            "Sample App", "http://www.sample.com", "sample@gmail.com");
    
    public static final ApiInfo DEFAULT_API_INFO = new ApiInfo(
            "Awesome API Title", "Awesome API Description", "1.0",
            "urn:tos", DEFAULT_CONTACT,
            "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", Arrays.asList());
    
    private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES =
            new HashSet<String>(Arrays.asList("application/json",
                    "application/xml"));
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(DEFAULT_API_INFO)
                .produces(DEFAULT_PRODUCES_AND_CONSUMES)
                .consumes(DEFAULT_PRODUCES_AND_CONSUMES);
     }
    }
    

    Now, access your swagger UI by going to this URL: http://localhost:8080/swagger-ui/index.html#/

    0 讨论(0)
  • 2020-12-02 00:29

    Conclusion: I find there are no jar files under maven repository ${user_home}/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2,after below steps everything is ok.

    I solved this issue by follow steps:

    • go to ${user_home}/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2
    • examine whether the files under 2.9.2 is complete。if there are missing files,delete the whole 2.9.2 directory
    • execute reimport all maven projects in intellij idea

    My spring boot version is 2.2.2.

    0 讨论(0)
  • 2020-12-02 00:30

    Add @RequestMapping("/") at the controller level, It works.

    0 讨论(0)
  • 2020-12-02 00:35

    For Spring Version >= 2.2, you should add the dependency springfox-boot-starter

    pom.xml:

    <properties>
        <java.version>1.8</java.version>
        <io.springfox.version>3.0.0</io.springfox.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-data-rest</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
    </dependencies>
    

    ApplicationSwaggerConfig

    @Configuration
    @EnableSwagger2
    public class ApplicationSwaggerConfig {
    
        @Bean
        public Docket employeeApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    
    }
    

    Swagger-UI link: http://localhost:8080/swagger-ui/index.html#/

    0 讨论(0)
  • 2020-12-02 00:35

    Already a lot of answers have already stated the right but still, there has been some confusion regarding the error.

    If you are using Spring Boot Version >= 2.2, it is recommended using SpringFox Swagger version 3.0.0

    Now, with only a single dependency is required to be added in the pom.xml.

    <!-- Swagger dependency -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    

    Once the application is started, you can get the documentation by hitting either of the new swagger URLs.

    Option 1: http://localhost:8080/swagger-ui/

    Option 2: http://localhost:8080/swagger-ui/index.html

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