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-docs
I'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
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01