开放 API 3 - 如何读取 Spring Boot 分页属性?

PAA*_*PAA 1 spring spring-boot openapi springdoc

我正在使用 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);
}
Run Code Online (Sandbox Code Playgroud)

因为我正在使用 Spring Boot 应用程序。我已经配置了以下配置,

# 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
Run Code Online (Sandbox Code Playgroud)

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

小智 6

我也在寻找这个,发现 SpringDoc 现在有一个注释来添加所需的参数,@PageableAsQueryParam。我在用着springdoc-openapi-ui-1.6.4

只需确保使用 隐藏Pageable对象@Parameter(hidden=true),因为它不会自动隐藏。

一个例子

@GetMapping("/customerQuery")
@PageableAsQueryParam
public List<PartyDTO> getCustomers(String name, @Parameter(hidden = true) Pageable pageable){
    return partyService.queryCustomer(name, pageable).getContent();
}
Run Code Online (Sandbox Code Playgroud)

将导致: 带有可分页参数的记录端点

使用的依赖项:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)