首页 > 解决方案 > What to use spring Restcontroller or RepositoryRestResource

问题描述

I need produce Rest Api with spring HAL hateoas support and pagination. What I see is if I use @Restcontroller I need manually write code for _links and paging logic.

If I use RepositoryRestResource we can get generated links and pagination but I don't have control on _link generation and API path, I don't know I can customize or not.

So how can we use @restcontroller and RepositoryRestResource together so that I can use pagination of repository rest resource and API path of rest controller

标签: javaspring-bootspring-data-jpaspring-data-restspring-rest

解决方案


您可以继续在您的存储库上使用@RepositoryRestResource,按照要点实现您所提到的,

  1. 使用org.springframework.data.repository.PagingAndSortingRepository扩展您的存储库,它将提供分页内容,您只需在请求参数中传递页面大小即可完成该工作。
  2. 可以在存储库中定义的方法之上使用 @RestResource(path = "your_path_to_api") 自定义 API 路径。

推荐阅读