java Spring自动添加“XTotalCount”头
我正在为我的Web应用程序使用“rest上的管理员”UI,它有下一个限制:
Note: The jsonServer REST client expects the API to include a X-Total-Count header in the response to GET_LIST calls. The value must be the total number of resources in the collection. This allows admin-on-rest to know how many pages of resources there are in total, and build the pagination controls.
我通过将X-Total-Count头添加到手动返回REST端点的列表中解决了此问题,如下所示:
response.addHeader("X-Total-Count", String.valueOf(outputList.size()));
但我想知道:是否有一些优雅的方式可以在春天自动完成?我的意思是,当某个端点返回JSON列表时,自动添加具有适当值的头
# 1 楼答案
因为从页面中可以得到全部元素。所以我给ResponseEntity添加了标题
getPagePost是一种服务方法,它在存储库中使用PageFindall(Pageable Pageable)
注意:如果内容范围不适合您,请将其更改为X-Total-Count
# 2 楼答案
是的,有!(如果您使用的是spring 4.1或更高版本)
它被称为ResponseBodyAdvice,它使您能够拦截调用(就在编写响应并允许访问原始http响应之前)
基本上,您需要实现如下控制器建议:
# 3 楼答案
如果您不想要响应中元素的总数,而是想要相应的JPA方法
PagingAndSortingRepository
中实体的总数,您可以做一些对分页应用程序有用的事情:)受Bohdan的答案启发(https://stackoverflow.com/a/44376133/986160)
确保调用默认方法的可分页版本,如它返回
Page
而不是List
:如果不使用存储库,则必须执行两个查询:
Select * from ...
和Select count(*) from ...
并返回一个Wrapper
,其中包含结果列表的内容,以及来自count的总数的总和。然后,您可以更改@ControllerAdvice
类,以期望您的Wrapper
,并从中获取总数,然后将其放在标题中