我正在尝试更改 API 文档的基本访问 URL。网址是“http://localhost:8080/swagger-ui.html”。我想得到类似“http://localhost:8080/myapi/swagger-ui.html ”的东西。
我使用 Springfox 2.8.0 Swagger、Java 8、Spring Boot 2.0 Swagger 的配置是:
@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);
}
}
自定义路径提供程序必须提供帮助,但我仍然可以通过使用 url“http://localhost:8080/swagger-ui.html”来访问 api 文档。如果我使用 url“http://localhost:8080/myapi/swagger-ui.html”,我会收到 404 错误。请看下面的屏幕截图。
最佳答案
UPD:Springfox 被遗弃
Springfox Swagger 一直是一个有点脏的解决方案,有很多不清楚和错误,但到现在(2021 年第四季度)它已经一年多没有更新了。
最后一根稻草是 Springfox Swagger 3.0 doesn't work anymore with Spring Boot 2.6.x.
因此,如果您正在阅读本文,请考虑切换到 https://springdoc.org/相反。
It's a pretty straightforward conversion and they do a great job of documenting it. https://springdoc.org/#migrating-from-springfox.
对于那些使用 Springfox Swagger 3.0.0 的人
Here's更改文档基本 url 的工作配置:
springfox:
documentation:
swaggerUi:
baseUrl: /documentation
openApi:
v3:
path: /documentation/v3/api-docs
swagger:
v2:
path: /documentation/v2/api-docs
关于java - 如何为 swagger 2.8.0 做友好的基本 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49924266/