Swagger with Spring Boot 2.0 leads to 404 error page

riorio :

I'm trying to integrate my Spring Boot version 2.0.1.RELEASE with Swagger.

From this blog post it seemed like it will be easy by just adding two Maven dependencies and everything should work.

So I added the following dependencies to the pom:

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

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

And created the SwaggerConfig bean:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
    Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build();

    return docket;
   }
}

And in the properties file I ended up with these 3 entries during the attempts to make it work:

spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service

But at the end, when accessing

http://localhost:8080/cat-service/api/v2/api-docs

or the UI page at

http://localhost:8080/cat-service/swagger-ui.html

I get a page not found error.

I found this issues in the swagger github page and this question in stackoverflow but I was not able to change my 404 error.

riorio :

I was able to make it work with Spring boot version 2.0.4.RELEASE and this blog post:

I added these dependencies:

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

And this configuration file:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

And it worked.

The Swagger UI can be reached at /swagger-ui.html#

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=37194&siteId=1