Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Following the instructions here:

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

I added these dependencies to my project:

compile "io.springfox:springfox-swagger2:2.7.0" compile "io.springfox:springfox-swagger-ui:2.7.0" 

and configured SpringFox Swagger like this:

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 SwaggerConfig {     @Bean     public Docket api() {         return new Docket(DocumentationType.SWAGGER_2)                 .select()                 .apis(RequestHandlerSelectors.any())                 .paths(PathSelectors.any())                 .build();     } } 

but the Swagger UI seems not to get enabled. I tried:

  • http://localhost:8080/swagger-ui.html
  • http://localhost:8080/api/swagger-ui.html
  • http://localhost:8080/v2/api-docs/swagger-ui.html

and all I get is:

Whitelabel Error Page  This application has no explicit mapping for /error, so you are seeing this as a fallback.  Mon Sep 11 09:43:46 BST 2017 There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported 

and on the logs I see:

2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported 2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported 

http://localhost:8080/swagger-resources returns:

[{"name": "default",   "location": "/v2/api-docs",   "swaggerVersion": "2.0"}] 

What am I missing?

like image 334
pupeno Avatar asked Sep 11 '17 08:09

pupeno


People also ask

Which dependency should be added to enable UI in Swagger?

Enabling Springfox's Swagger UI You need to add one additional dependency to the pom. xml to use Swagger UI. Now you can get the Swagger UI using http://localhost:8080/swagger-ui.html#/.

How do I get the swagger UI?

Go to http://localhost:8000/ in your address bar. This address lets you view the local web server. By default, web servers default to the index. html file in the directory, so it will show the Swagger UI file automatically.

What is Springfox swagger UI?

Springfox is a framework that acts as the “glue” between Swagger and Spring. It generates the specification (contract) based on your code and also deploys the Swagger UI client with your application, allowing you to immediately test your REST API.


1 Answers

Springfox 3.0.0 only works with Spring Boot <= 2.6.0-M2 but not with versions above it

Options for Spring Boot 2.6 M2 <
1. spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER #-> App.properties
    -    But without Actuator
2. @EnableWebMvc
3. Migrate to springdoc-openapi-ui - same steps as io.springfox >= 3.X

io.springfox >= 2.X

io.springfox >= 3.X

<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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
browser URL
http://localhost:8080/swagger-ui.html
browser URL
http://localhost:8080/swagger-ui/
http://localhost:8080/swagger-ui/index.html
Must Need

mvn clean

Must Need

mvn clean

@Configuration
@EnableSwagger2
@Configuration
@EnableSwagger2
like image 147
Ravi Parekh Avatar answered Sep 24 '22 05:09

Ravi Parekh