how to do friendly base url for swagger 2.8.0(如何为 swagger 2.8.0 做友好的基本 url)
问题描述
我正在尝试更改 API 文档的基本访问 URL.网址是
UPD:Springfox 被放弃
Springfox Swagger 一直是一个有点脏的解决方案,有很多不明确和错误,但到现在(2021 Q4)它已经一年多没有更新了.
最后一根稻草是 Springfox Swagger 3.0 不再适用于 Spring Boot 2.6 的事实.x.
因此,如果您阅读本文,请考虑改用 https://springdoc.org/.
<块引用>这是一个非常简单的转换,他们做得很好记录它.https://springdoc.org/#migrating-from-springfox.
对于使用 Springfox Swagger 3.0.0 的用户
这是更改文档的基本网址的工作配置:
springfox:文档:招摇:baseUrl:/文档开放API:v3:路径:/documentation/v3/api-docs昂首阔步:v2:路径:/documentation/v2/api-docsI'm trying to change base access url for API documentation. The url is "http://localhost:8080/swagger-ui.html". I want to get something like "http://localhost:8080/myapi/swagger-ui.html".
I use Springfox 2.8.0 Swagger, Java 8, Spring Boot 2.0 The swagger configuration is:
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket api(ServletContext servletContext) {
return new Docket(DocumentationType.SWAGGER_2)
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/myapi";
}
})
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error")))
.build()
.useDefaultResponseMessages(false);
}
}
Custom path provider had to help, but I still get access to api documentation by using url "http://localhost:8080/swagger-ui.html". If I use url "http://localhost:8080/myapi/swagger-ui.html", I get 404 error. Look at the screenshot below.
UPD: Springfox is abandoned
Springfox Swagger had always been kinda dirty solution with a lot of unclearness and bugs, but by now (2021 Q4) it hadn't been updated for more than a year.
The final straw was the fact that Springfox Swagger 3.0 doesn't work anymore with Spring Boot 2.6.x.
So, if you reading this, please, consider switching over to https://springdoc.org/ instead.
It's a pretty straightforward conversion and they do a great job of documenting it. https://springdoc.org/#migrating-from-springfox.
For those who use Springfox Swagger 3.0.0
Here's the working configuration for changing base url for docs:
springfox:
documentation:
swaggerUi:
baseUrl: /documentation
openApi:
v3:
path: /documentation/v3/api-docs
swagger:
v2:
path: /documentation/v2/api-docs
这篇关于如何为 swagger 2.8.0 做友好的基本 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 swagger 2.8.0 做友好的基本 url
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
