我最近将springfox转换为springdoc openapi,为spring boot rest api服务生成openapi。
在我添加安全方案之前,一切都很顺利。一旦我这样做了,我的方案就不会再出现了,并且在swaggerui页面上会出现一个错误:
Could not resolve reference: Could not resolve pointer: /components/schemas/Ping does not exist in document
我正在以编程方式设置配置,有两个组。
我正在使用SpringBootV2.4.0和SpringDocOpenAPI ui v1.5.1
my pom.xml的片段:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-hateoas</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.5.1</version>
</dependency>
配置中的代码段:
@Bean
public GroupedOpenApi apiV1() {
String[] paths = {"/v1/**"};
String[] packagesToScan = {"com.test.controller"};
return GroupedOpenApi.builder()
.group("v1")
.packagesToScan(packagesToScan)
.pathsToMatch(paths)
.addOpenApiCustomiser(buildV1OpenAPI())
.build();
}
@Bean
public GroupedOpenApi apiV2() {
String[] paths = {"/v2/**"};
String[] packagesToScan = {"com.test.controller"};
return GroupedOpenApi.builder()
.group("v2")
.packagesToScan(packagesToScan)
.pathsToMatch(paths)
.addOpenApiCustomiser(buildV2OpenAPI())
.build();
}
public OpenApiCustomiser buildV1OpenAPI() {
return openApi -> openApi.info(apiInfo().version("v1"));
}
public OpenApiCustomiser buildV2OpenAPI() {
final String securitySchemeName = "Access Token";
return openApi -> openApi.info(apiInfo().version("v2"))
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
.components(new Components().addSecuritySchemes(securitySchemeName, new SecurityScheme()
.type(SecurityScheme.Type.APIKEY)
.in(SecurityScheme.In.HEADER)
.name(HttpHeaders.AUTHORIZATION)));
}
// Describe the apis
private Info apiInfo() {
return new Info()
.title("Title")
.description("API Description");
}
对我的v1组来说,一切正常。我的模式出现在swaggerui页面上,我在生成的api文档的components部分看到它们。
"components": {
"schemas": {
"ApplicationErrorResponse": {
...
}
},
"Ping": {
...
}
}
}
对于我的v2组,不会生成模式。
"components": {
"securitySchemes": {
"Access Token": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}
你知道为什么在以编程方式将安全方案添加到openapi组件时,不会自动扫描和添加我的模式吗?我的配置有什么遗漏吗?
这是我的控制器中的请求Map。
@Operation(summary = "Verify API and backend connectivity",
description = "Confirm connectivity to the backend, as well and verify API service is running.")
@OkResponse
@GetMapping(value = API_VERSION_2 + "/ping", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Ping> getPingV2(HttpServletRequest request) {
... }
下面是我的@okresponse注解:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@ApiResponse(responseCode = HTTP_200,
description = HTTP_200_OK,
headers = {
@Header(name = CONTENT_VERSION_HEADER, description = CONTENT_VERSION_HEADER_DESCRIPTION, schema = @Schema(type = "string")),
@Header(name = DEPRECATION_MESSAGE_HEADER, description = DEPRECATION_MESSAGE_HEADER_DESCRIPTION, schema = @Schema(type = "string")),
@Header(name = DESCRIPTION_HEADER, description = DESCRIPTION_HEADER_DESCRIPTION, schema = @Schema(type = "string"))
})
public @interface OkResponse {
}
我的v1Map定义类似。
1条答案
按热度按时间1szpjjfi1#
因此,当仅仅依赖openapicustomiser来创建openapi时,被扫描的组件被忽略,或者至少被自定义程序中指定的组件覆盖(我也可以通过编程方式添加所有模式,但是维护起来会非常麻烦)。
将我的配置更改为以下解决了我的问题:
虽然这在技术上也会将authorize按钮和安全方案添加到v1组中,但是可以忽略它,因为这些api端点无论如何都不安全(内部api,它们应该很快就会消失)。
可能是一个更好的解决方案,因为信息基本上是相同的组之间。