如何全局忽略 API of Spring Boot from Open API 3 规范?
How to Globally ignore API of Spring Boot from Open API 3 specification?
我已经阅读了文档:https://springdoc.github.io/springdoc-openapi-demos/faq.html#how-can-i-ignore-some-field-of-model-,但是文档不是很清楚,我有 Spring Boot REST HATEOAS 实现项目并使用 打开API 3规范而不是Swagger.
我为每个端点实现了分页,但我的行业标准如何将内容期望为复数内容。但由于这是 Pageable API 的一部分,我无法覆盖它,而是希望禁用它。有什么建议吗?
PageEmployeeOut:
type: object
properties:
totalElements:
type: integer
format: int64
totalPages:
type: integer
format: int32
size:
type: integer
format: int32
content:
type: array
items:
$ref: '#/components/schemas/EmployeeOut'
number:
type: integer
format: int32
sort:
$ref: '#/components/schemas/Sort'
numberOfElements:
type: integer
format: int32
first:
type: boolean
pageable:
$ref: '#/components/schemas/Pageable'
last:
type: boolean
empty:
type: boolean
就像在 Springfox Swagger 中一样,我们可以像下面那样做,它在 Open API 3 (springdoc-openui) 中的等效项是什么?
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.ignoredParameterTypes(Pageable.class);
}
这是我的端点
public ResponseEntity<Page<EmployeeDto>> findEmployees(@Parameter(hidden=true) Pageable pageable) {
Page<EmployeeDto> page = employeeService.findAllEmployees(page_params, pageable);
return new ResponseEntity<>(page, HttpStatus.OK);
}
你只需要添加你想要的类型的OpenAPI描述
让我们支持你想要 return EmployeeDto 而不是 Page
@ApiResponse(responseCode = "200", description = "successful operation", content = @Content(schema = @Schema(implementation = EmployeeDto.class)))
如果您需要在您的应用程序中全局替换它,您只需使用标准的 ModelConverter:
@Component
public class PageSupportConverter implements ModelConverter {
@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = Json.mapper().constructType(type.getType());
if (javaType != null) {
Class<?> cls = javaType.getRawClass();
if (Page.class.isAssignableFrom(cls)) {
JavaType innerType = javaType.getBindings().getBoundType(0);
if (innerType.getBindings() != null && isResponseTypeWrapper(innerType.getRawClass())) {
type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).resolveAsRef(true);
return this.resolve(type, context, chain);
}
else {
type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).resolveAsRef(true);
}
}
}
if (chain.hasNext()) {
return chain.next().resolve(type, context, chain);
}
else {
return null;
}
}
}
我已经阅读了文档:https://springdoc.github.io/springdoc-openapi-demos/faq.html#how-can-i-ignore-some-field-of-model-,但是文档不是很清楚,我有 Spring Boot REST HATEOAS 实现项目并使用 打开API 3规范而不是Swagger.
我为每个端点实现了分页,但我的行业标准如何将内容期望为复数内容。但由于这是 Pageable API 的一部分,我无法覆盖它,而是希望禁用它。有什么建议吗?
PageEmployeeOut:
type: object
properties:
totalElements:
type: integer
format: int64
totalPages:
type: integer
format: int32
size:
type: integer
format: int32
content:
type: array
items:
$ref: '#/components/schemas/EmployeeOut'
number:
type: integer
format: int32
sort:
$ref: '#/components/schemas/Sort'
numberOfElements:
type: integer
format: int32
first:
type: boolean
pageable:
$ref: '#/components/schemas/Pageable'
last:
type: boolean
empty:
type: boolean
就像在 Springfox Swagger 中一样,我们可以像下面那样做,它在 Open API 3 (springdoc-openui) 中的等效项是什么?
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.ignoredParameterTypes(Pageable.class);
}
这是我的端点
public ResponseEntity<Page<EmployeeDto>> findEmployees(@Parameter(hidden=true) Pageable pageable) {
Page<EmployeeDto> page = employeeService.findAllEmployees(page_params, pageable);
return new ResponseEntity<>(page, HttpStatus.OK);
}
你只需要添加你想要的类型的OpenAPI描述 让我们支持你想要 return EmployeeDto 而不是 Page
@ApiResponse(responseCode = "200", description = "successful operation", content = @Content(schema = @Schema(implementation = EmployeeDto.class)))
如果您需要在您的应用程序中全局替换它,您只需使用标准的 ModelConverter:
@Component
public class PageSupportConverter implements ModelConverter {
@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = Json.mapper().constructType(type.getType());
if (javaType != null) {
Class<?> cls = javaType.getRawClass();
if (Page.class.isAssignableFrom(cls)) {
JavaType innerType = javaType.getBindings().getBoundType(0);
if (innerType.getBindings() != null && isResponseTypeWrapper(innerType.getRawClass())) {
type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).resolveAsRef(true);
return this.resolve(type, context, chain);
}
else {
type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).resolveAsRef(true);
}
}
}
if (chain.hasNext()) {
return chain.next().resolve(type, context, chain);
}
else {
return null;
}
}
}