我有一个位于反向代理后面的 Spring Boot 应用程序。该代理重写路径。 在我的应用程序中,我有一个 REST 控制器并使用 springdoc 记录它
@Operation(summary = "Expose some data", ...)
@RequestMapping(path = "/a", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public MyDto getA() {
springdoc / OpenAPI 生成导致可在
/a
处进行操作
然而,反向代理使其在 /b
可用
我如何告诉 springdoc 它不应该使用
@RequestMapping
注释中的内省路径,而是传递到任何 springdoc 注释的路径?
谢谢。
只是为了让遇到同样问题的开发人员更容易:在下面您可以找到一个有关如何使用
OpenApiCustomizer
覆盖操作路径的示例。它可以直接在 OpenAPI
上设置,也可以按组覆盖。是用Kotlin写的,不过Java应该也差不多。
@Bean
fun myGroup(): GroupedOpenApi {
val servers = listOf(
Server().url("https://my-server.de/test").description("Test Server")
)
return GroupedOpenApi.builder()
.group("myGroup")
.packagesToScan("de.xxx.test")
.addOpenApiCustomizer { openAPI ->
openAPI.apply {
info.version("1.0.4")
servers = servers
removePathPrefixes(this, "/api/v1/test")
}
}
.build()
}
private fun removePathPrefixes(openAPI: OpenAPI, pathPrefix: String) {
val newPaths = openAPI.paths.mapKeys { it.key.substringAfter(pathPrefix) }
openAPI.paths.clear()
newPaths.forEach {
openAPI.paths.addPathItem(it.key, it.value)
}
}
我也有同样的问题。我的控制器上有路径
/history
,
但我想通过端点上的 openApi 文档公开它 /domain/history
。
我的解决方案是一个有点解决方法,但有效:
swagger.yaml 示例:
openapi: 3.0.3
info:
title: API
version: 1.0.0
description: API documentation
paths:
/domain/history:
get:
summary: Retrieve domain history
通过将 'hidden=true' 添加到现有 @Operation 注释来隐藏原始端点文档:
@Operation(
summary = "Retrieve domain history",
description = "Retrieves hisotry",
hidden = true
)
OpenApi配置:
@OpenAPIDefinition(servers = {@Server(url = "${app.server-url}", description = "Default Server URL")})
@Configuration
public class OpenApiConfiguration {
private final static String SECURITY = "security";
@Bean
public OpenAPI baseOpenApi() throws IOException {
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
ClassPathResource resource = new ClassPathResource("swagger.yaml");
String yamlString = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
OpenAPI openAPI = yamlReader.readValue(yamlString, OpenAPI.class);
openAPI
.addSecurityItem(new SecurityRequirement()
.addList(SECURITY))
.components(new Components()
.addSecuritySchemes(SECURITY, new SecurityScheme()
.name(SECURITY)
.type(SecurityScheme.Type.HTTP)
.scheme("")
.bearerFormat("JWT")));
return openAPI;
}