本文介绍了添加了 Springfox Swagger-UI 但它不起作用,我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按照此处的说明进行操作:
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"
并像这样配置SpringFox Swagger:
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();
}
}
但 Swagger UI 似乎没有启用.我试过了:
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
我得到的是:
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
在我看到的日志上:
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 返回:
[{"name": "default",
"location": "/v2/api-docs",
"swaggerVersion": "2.0"}]
我错过了什么?
推荐答案
引自 https://github.com/springfox/springfox/issues/1672
当 spring 找到一个只有一个变量 swagger 的简单路径时,无法拦截 URL.
When spring finds a simple path with just one variable swagger cannot intercept the URLs.
通过调查评论中的各种想法发现.
Found by investigating various ideas in comments.
这篇关于添加了 Springfox Swagger-UI 但它不起作用,我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!