添加了 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"}]
我错过了什么?
正确答案
我遇到了这个问题,因为我有带有请求映射的端点,这些端点具有这种形式的路径变量:/{var}.事实证明,这对于 GET 和 POST 端点(即 GET/{var} 和 POST/{var} 块 swagger-ui)都是一个问题.一旦我使路径更具体,我就可以使用 swagger-ui.
I ran into this issue because I had endpoints with request mappings that had path variables of this form: /{var}. Turns out that this is an issue for both GET and POST endpoints i.e. GET /{var} and POST /{var} block swagger-ui. Once I made the paths more specific, I got swagger-ui to work.
引用自 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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfiahe
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20