Open API 3-如何读取Spring Boot分页属性? [英] Open API 3 - How to read Spring Boot Pagination Properties?

查看:426
本文介绍了Open API 3-如何读取Spring Boot分页属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot + Spring Rest分页+ Open API 3.

I am using Spring Boot + Spring Rest Pagination + Open API 3.

@Operation(summary = "Find Contacts by name", description = "Name search by %name% format", tags = { "contact" })
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "successful operation", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Contact.class)))) })

@Parameter(in = ParameterIn.QUERY, description = "Zero-based page index (0..N)", name = "page"
, content = @Content(schema = @Schema(type = "integer", defaultValue = "0")))
@Parameter(in = ParameterIn.QUERY, description = "The size of the page to be returned", name = "size"
, content = @Content(schema = @Schema(type = "integer", defaultValue = "20")))
@Parameter(in = ParameterIn.QUERY, description = "Sorting criteria in the format: property(,asc|desc). "
        + "Default sort order is ascending. " + "Multiple sort criteria are supported."
        , name = "sort", content = @Content(array = @ArraySchema(schema = @Schema(type = "string"))))
@GetMapping(value = "/contacts")
public ResponseEntity<List<Contact>> findAll(Pagination pagination) {
    List<Contact> contacts = new ArrayList<>();
    contacts.add(Contact.builder().address1("Address1").address2("Address2").build());
    return new ResponseEntity<>(contacts, HttpStatus.OK);
}

由于我正在使用Spring Boot Application.我已经配置了以下配置,

Since I'm using Spring Boot Application. I've configured below configurations,

# Added for Pagination 
spring.data.web.pageable.default-page-size=25
spring.data.web.pageable.page-parameter=page
spring.data.web.pageable.size-parameter=size 
spring.data.web.sort.sort-parameter=sort

有什么方法可以为Open API 3规范配置上述属性,而不是对其进行硬编码?

Is there any way we can configure above properties for the Open API 3 specification instead of making it hard-coded ?

推荐答案

支持spring-data-commons的Pageable. 使用Pageable类型的项目应将此依赖项与springdoc-openapi-ui依赖项一起添加.

The support for Pageable of spring-data-commons is available. The projects that use Pageable type should add this dependency together with the springdoc-openapi-ui dependency.

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-data-rest</artifactId>
  <version>latest.version</version>
</dependency>

有关常见问题的更多详细信息,请参见:

More details are available on the F.A.Q:

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆