Spring MVC 中的分页 RESTful API 响应

分页允许您将来自Spring MVC的大型RESTful API响应拆分为称为页面的较小块。在这篇文章中,让我们看看如何使用Spring MVC和Spring JPA对来自Spring boot应用程序的JSON响应进行分页。

Spring MVC 中的分页和排序

如前所述,我们可以使用spring 数据 JPA 实现分页和排序。Spring MVC通过直接从Web层注入可分页对象将其提升到一个新的水平。

Spring MVC 允许您传递以下可选查询参数,以在请求范围内构建可分页对象。我们可以在 JPA 存储库方法中进一步使用这些可分页对象。因此,让我们看看如何实现它们。

为 Web 和 JPA 添加依赖关系

在本例中,我们使用 SpringData JPA 和 SpringWeb 组件作为依赖项。


org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web

Code language: HTML, XML (xml)

编写可分页的 JPA 存储库方法

接下来,我们需要编写一个以页面形式返回数据库记录的存储库。我们已经在上一篇文章中详细介绍了如何编写和使用它们。在我们的例子中,我们使用的是Account类。


@Entity public class Account { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Integer id; String accountNumber; String fullName; @JsonBackReference @ManyToOne Branch branch; BigDecimal balance; // getters and setters }

Code language: JavaScript (javascript)


public interface AccountRepository extends JpaRepository { @Query("select a from Account a") Page findAllAccounts(Pageable pageable); }

Code language: PHP (php)

We will use this JPA pagination implementation in our service to pass the pageable from the Spring MVC controller.

Write a Spring MVC controller with Pageable parameter

Next, we can write an AccountService and AccountController that uses the AccountRepository interface.


@Service public class AccountService { private final AccountRepository accountRepository; public AccountService(AccountRepository accountRepository) { this.accountRepository = accountRepository; } public Page getAccounts(Pageable pageable) { return accountRepository.findAllAccounts(pageable); } }

Code language: PHP (php)

Now the spring MVC controller class with pagination.


@RestController public class AccountController { private final AccountService accountService; public AccountController(AccountService accountService) { this.accountService = accountService; } @GetMapping("/accounts") Page getAccounts(Pageable pageable) { return accountService.getAccounts(pageable); } }

Code language: PHP (php)

如您所见,响应本身是一个Page对象。让我们运行应用程序,看看这个实现是如何工作的。

Spring MVC 中的分页 RESTful API 响应_第1张图片

如此处所示,响应包含一个内容字段,其中包含 20 条客户记录。它还带有当前与页面相关的元数据。从这里,客户端可以在前端显示总元素、页数、当前页面等。

但请记住,我们没有添加任何查询参数。但是,Spring MVC 仍然使用分页,因为默认情况下,spring 将页面大小设置为 20,而不进行任何排序。

春季 MVC 分页参数

您可以传递以下参数来覆盖页面行为。

  1. page– 表示要请求的页码(从 0 开始,默认为 0)
  2. size– 结果中要返回的元素数(默认为 20)
  3. sort– 表示要排序的字段的字符串列表。(包括排序方向ASC,DESC)

因此,让我们检查一下这些参数的实际效果。要查看特定页面,您可以执行以下操作。


http://localhost:8080/accounts?page=2

Code language: JavaScript (javascript)

如果要指定页面上的元素数,则可以传递size参数


http://localhost:8080/accounts?page=2&size=15

Code language: JavaScript (javascript)

如果您决定按字段对对象进行排序,则还可以传递具有可选排序方向的排序参数。


http://localhost:8080/accounts?page=2&size=15&sort=balance,DESC

Code language: JavaScript (javascript)

您甚至可以使用多个排序参数对两个字段进行排序。


http://localhost:8080/accounts?page=2&size=15&sort=balance,DESC&sort=id,ASC

Code language: JavaScript (javascript)

用于分页和排序的春季 MVC 配置

Spring MVC提供了某些配置选项来调整分页的行为。以下是您需要了解的重要内容。

如果您的 API 已有页面或大小参数,则可以更改参数名称,如下所示。


spring.data.web.pageable.page-parameter=chunk spring.data.web.pageable.size-parameter=limit

您还可以通过向可分页参数添加前缀来更改参数名称。


spring.data.web.pageable.prefix=page

这样,请求应该是类似/accounts?page_chunk=1&page_limit=30

如果您希望页面从 1 而不是 0 开始,那么您可以使用以下配置。


spring.data.web.pageable.one-indexed-parameters=true

Code language: JavaScript (javascript)

此外,默认情况下,size参数的值最多可以为 2000。默认页面大小为 20。根据您的要求,如果要增加或减少它,则应使用以下配置。


spring.data.web.pageable.default-page-size=30 spring.data.web.pageable.max-page-size=500

Code language: PHP (php)

总结

总而言之,我们学习了如何使用Spring MVC和Spring Data JPA在RESTful Web服务中实现分页。您可以在我们的GitHub 存储库中找到上述示例。

相关

  • 春季 JPA 中的分页和排序
  • 带有 Spring 引导的 RESTful JPA 存储库
  • 带有示例的 Spring 引导文件上传
  • 春季数据JPA简介
  • Spring 安全性中的自定义登录表单

 

你可能感兴趣的:(spring,mvc,restful)