简体   繁体   English

如何从 Open API 3 规范全局忽略 Spring Boot 的 API?

[英]How to Globally ignore API of Spring Boot from Open API 3 specification?

I went through the documentation: https://springdoc.github.io/springdoc-openapi-demos/faq.html#how-can-i-ignore-some-field-of-model- already, but documents are not very clear, I have Spring Boot REST HATEOAS implementation project and using Open API 3 specification instead of Swagger .我浏览了文档: https : //springdoc.github.io/springdoc-openapi-demos/faq.html#how-can-i-ignore-some-field-of-model-已经,但文档不是很清楚,我有Spring Boot REST HATEOAS实现项目并使用Open API 3 规范而不是 Swagger

I've Pagination implemented for each endpoints, but some how my industry standards expecting content as a plural contents.我已经为每个端点实现了分页,但我的行业标准如何将内容作为复数内容。 But since this is part of Pageable API I am not able to override it, instead looking to disable it.但由于这是 Pageable API 的一部分,我无法覆盖它,而是希望禁用它。 Any suggestion how can we do that ?有什么建议我们怎么做?

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

Like in Springfox Swagger we can do like below, what is the equivalent of it in Open API 3 (springdoc-openui) ?就像在 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);
}

This is my endpoint这是我的终点

public ResponseEntity<Page<EmployeeDto>> findEmployees(@Parameter(hidden=true) Pageable pageable) {
    Page<EmployeeDto> page = employeeService.findAllEmployees(page_params, pageable);
    return new ResponseEntity<>(page, HttpStatus.OK);
}

You just need to add the OpenAPI description of the type you want ov Lets support you want return EmployeeDto instead of Page你只需要添加你想要的类型的 OpenAPI 描述 ov Lets support you want return EmployeeDto 而不是 Page

@ApiResponse(responseCode = "200", description = "successful operation", content = @Content(schema = @Schema(implementation = EmployeeDto.class)))

If you need it to be replaced globally in your application, you just use the standard ModelConverter:如果您需要在您的应用程序中全局替换它,您只需使用标准的 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 忽略来自 Spring 引导 API 响应的审核字段 - Ignore Auditing fields from Spring Boot API Response 使用 spring boot 规范 API 基于计数的排序结果 - Order results based on count using spring boot specification API 打开 API 3 - 如何读取 Spring 引导分页属性? - Open API 3 - How to read Spring Boot Pagination Properties? 如何从另一个新的 Spring 启动项目中调用 Spring 启动项目中的 Spring 启动 api - How to call a Spring boot api present in one Spring boot project from another new Spring boot project 如何在spring引导(打开api v3)中为swagger ui指定api docs url? - How to specify api docs url for swagger ui in spring boot (open api v3)? 如何使用 springdoc-openapi 将 Open API 3 与 Spring 项目(不是 Spring Boot)集成 - How to Integrate Open API 3 with Spring project (not Spring Boot) using springdoc-openapi 如何在 Spring Boot 中持续接收和解析来自 REST API 的 JSON - How to continuously receive and parse the JSON from REST API in spring boot 如何在春季启动时从同一应用程序调用另一个API - How to call another api from same app in spring boot 如何从Apache的Angular中调用Spring Boot API - how to call spring boot api from angular in apache 如何使用Spring Boot应用程序从Rest API返回HTML - How to return html from Rest API with spring boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM