我正在尝试更改 swagger codumentation 的基本路径。目前我有
@RequestMapping(path = "/api/resourceName/v1")
和 Swagger 配置
return new Docket(DocumentationType.SWAGGER_2).
select()
.apis(RequestHandlerSelectors.basePackage("com.company"))
.paths(PathSelectors.ant("/api/**"))
.build()
.apiInfo(apiInfo());
这将 swagger 基本路径指定为 "basePath": "/"
我想将基本路径添加为 "basePath": "/api"
所以我遵循了这样的 diff 线程 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
。
当我通过 postman 发送 /resourceName.v1
的请求时,它就可以工作了。
因此 api 注册为 /resourceName/v1
,并且基础只是由 swagger 添加在其之上,如果请求通过 swagger UI 发送,则该基础将不起作用
然后我将 server.servlet-path=/api
添加到 application.properties
以在请求映射中注册基本路径,现在 swagger 将基本路径显示为 /api
无需额外配置。
但问题是现在 swagger 文档可以在 http://localhost:8080/api/swagger-ui.html
上找到,而不是 http://localhost:8080/swagger- ui.html
。由于我们在 http://host/swagger-ui.html
上有所有其他服务文档,因此这没有用。
有什么方法可以添加基础并仍然访问 http://host/swagger-ui.html
上的文档,并且 api 的工作按 swagger 和 postman 的预期进行
最佳答案
是的,您可以为所有 swagger 请求添加基本路径。我为此目的使用了以下配置:
@Configuration
@EnableSwagger2
public class SpringFoxConfig {
@Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
docket.pathMapping("api");
return docket;
}
}
希望这有帮助。
关于spring-boot - 添加基本路径到 swagger 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60660198/