【发布时间】:2020-06-24 20:43:38
【问题描述】:
我正在尝试更改 swagger codumentation 的基本路径。目前我有
@RequestMapping(path = "/api/resourceName/v1")
和大摇大摆的配置
return new Docket(DocumentationType.SWAGGER_2).
select()
.apis(RequestHandlerSelectors.basePackage("com.company"))
.paths(PathSelectors.ant("/api/**"))
.build()
.apiInfo(apiInfo());
这是给大摇大摆的基本路径"basePath": "/"
我想将基本路径添加为"basePath": "/api",所以我按照How to change basePath for Springfox Swagger 2.0 这样的差异线程添加了
return new Docket(DocumentationType.SWAGGER_2).
select()
.apis(RequestHandlerSelectors.basePackage("com.company"))
.paths(PathSelectors.ant("/api/**"))
.build()
.apiInfo(apiInfo())
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/api";
}
});
现在基本路径更改为"basePath": "/api",并且我将路径映射更新为@RequestMapping(path = "/resourceName/v1"),因为已添加基本路径。
当我从 swagger 发送请求时,请求将发送到 /api/resourceName/v1,但服务返回 404。
当我通过邮递员发送/resourceName.v1 的请求时,它可以工作。
所以 api 被注册为/resourceName/v1 并且 base 只是由 swagger 在它上面添加的,如果请求通过 swagger UI 发送将不起作用
然后我将server.servlet-path=/api添加到application.properties以在请求映射中注册basepath,现在swagger将基本路径显示为/api,无需额外配置。
但问题是现在可以在http://localhost:8080/api/swagger-ui.html 而不是http://localhost:8080/swagger-ui.html 上获得swagger 文档。因为我们在http://host/swagger-ui.html 有我们所有的其他服务文档,所以这没什么用。
有什么方法可以添加基础并仍然可以访问http://host/swagger-ui.html 的文档,并且 api 的工作与 swagger 和 postman 的预期一样
【问题讨论】:
标签: spring-boot swagger swagger-ui swagger-2.0