当前位置: 首页 > 知识库问答 >
问题:

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

舒飞捷
2023-03-14
@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应用程序。我配置了以下配置,

# 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 API3规范配置上面的属性而不是硬编码它吗?

共有1个答案

毋举
2023-03-14

spring-data-commons对可分页的支持是可用的。如果只想启用spring分页类型的支持,可以使用以下方法启用:

SpringDocUtils.getConfig().replaceWithClass(org.springframework.data.domain.Pageable.class, 
org.springdoc.core.converters.models.Pageable.class);

或者,使用可分页类型的项目可以将follwing依赖项与springdoc-openapi-ui依赖项一起添加,这将支持spring.data.rest.default...属性和@PageableDefault注释:

请注意,latest.version应该至少等于V1.4.5,它增加了这个新特性

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-data-rest</artifactId>
  <version>latest.version</version>
</dependency>
    null
 类似资料:
  • 我是Java和Spring Boot的初学者,我在Spring Boot上使用< code>Pagination,用这个代码我返回用户列表,如果我想返回页数,我必须这样做? 我知道使用我可以获取页数,但如何返回它?

  • 我使用自动配置(仅通过注释)运行Spring启动和kafka,并在.yaml文件中定义了道具,即: 它工作得很好,spring maps即字段group-id正确。 但是当我尝试使用相同的yaml文件手动配置Kafka(使用消费者工厂和消费者配置)时,我遇到了问题。在类中,消费者配置Kafka属性以 命名。在名称中,而不是 _ 即: 所以我不能把它们加载到map中,然后把map传递给Consume

  • 我有一个带有Spring执行器的简单项目,还有一个生成git.properties(驻留在classes目录中)的maven插件。 然而,当我运行我的应用程序时,/acturet/info request显示: 文档说明autoconfigure应该自动获取属性https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

  • 我想要log4j2Spring。xml从应用程序中读取属性。属性文件。但似乎是Spring。xml无法读取此内容。我读过https://logging.apache.org/log4j/2.x/manual/lookups.html#SpringLookup为了实现这一点。 我在这个网站上看到了这个答案。我也这样试过。但这对我没有帮助。 我的身材。格雷德尔是这样的: application.pro

  • 为了简化这个问题,我们有一个类/餐桌酒(餐桌“wines”),除其他属性外,它具有: null 我试图在我的存储库中创建一个搜索方法,以供RestController使用。 RestController中的方法声明如下所示: 我现在要做的是:为数据库创建一个查询,如果给出了searchTerm,就使用该数据库,与Origin相同。并且应该是可分页的。示例: 这(除了超级丑,特别是对于更多属性)很可

  • 因此,我一直在试图弄清楚如何在两个应用程序中使用环境变量。属性或application.yaml。由于这在无数的框架中都能工作(例如Laravel),所以我认为这在Spring Boot中也能工作。现在我发现,情况并非如此。 我看到一篇关于Spring Environment API的帖子,它开始说环境变量SPRING_DATASOURCE_URL将等同于在我的应用程序中设置spring.data