Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger with Spring Boot 2.0 leads to 404 error page

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.

like image 367
riorio Avatar asked May 31 '18 06:05

riorio


People also ask

What is Swagger 2 in Spring boot?

Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser. To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file.

What is 404 error in spring boot?

As with any web application or website, Spring MVC returns the HTTP 404 response code when the requested resource can't be found.

How do I change the default Swagger spring boot URL?

You can change default swagger-ui path programmatically using ApplicationListener<ApplicationPreparedEvent> . The idea is simple - override springdoc. swagger-ui. path=/custom/path before your Spring Boot application starts.

How do I disable Swagger in spring boot?

In Spring, we can use the @Profile annotation to enable or disable the injection of beans. This forces us to be explicit about environments where we want to activate Swagger. It also helps to prevent accidentally turning it on in production.


1 Answers

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#

like image 176
riorio Avatar answered Sep 20 '22 06:09

riorio