[英]Rest Controller response Page<ObjectDto> returning empty object
I have used Spring Data Pageable to implement paging and sorting. 我已经使用Spring Data Pageable实现分页和排序。 My relevant code snippet is:
我相关的代码段是:
//GET API designed as POST due to Request Body, in lines of ES APIs
@PostMapping(value = "/v2")
public Page<ObjectDto> getObjects(@RequestBody @Valid Map<Integer, FilterRequestDto> filterDtoMap, Pageable pageable) {
Page<ObjectDto> objectDtos = myService.getDtos(filterDtoMap, pageable);
log.info(String.valueOf(objectDtos.getTotalElements()));
return objectDtos;
}
Although I have data(confirmed from the log), the above API is responding with {} . 尽管我有数据(从日志中确认),但上述API仍以{}响应。
However, when I am returning List like in this snippet: 但是,当我像下面的代码片段那样返回List时:
@PostMapping(value = "/v2")
public List<ObjectDto> getObjects(@RequestBody @Valid Map<Integer,FilterRequestDto> filterDtoMap, Pageable pageable) {
Page<ObjectDto> objectDtos = myService.getDtos(filterDtoMap, pageable);
log.info(String.valueOf(objectDtos.getTotalElements()));
return objectDtos.getContent();
}
Response for List return type for the exact same API is a proper response as expected: 完全相同的API的列表返回类型的响应是预期的正确响应:
[ { "sellingPrice": 23000, "id": 1, "version": 1, "createdOn": "25 Feb 2019,05:53", "lastUpdatedOn": "25 Feb 2019,05:53" } ] [{“ sellingPrice”:23000,“ id”:1,“ version”:1,“ createdOn”:“ 25 Feb 2019,05:53”,“ lastUpdatedOn”:“ 25 Feb 2019,05:53”}]
Repository Code: 仓库代码:
@Repository
public interface MyObjectRepository extends JpaRepository<MyObject, Long>, JpaSpecificationExecutor<MyObject> {}
Service Code: 服务代码:
public Page<ObjectDto> getTyres(Map<Integer, FilterRequestDto> filterDtoMap, Pageable pageable) {
List<FilterDto> filterDtoList = filtersService.getAllFiltersById(filterDtoMap.keySet());
return objectMapper.toDto(myObjectRepository.findAll(ObjectSpecs.filter(filterDtoList, filterDtoMap), pageable));
}
As far as I know, we can return page as a Rest API response, so what can be the issue here? 据我所知,我们可以将页面作为Rest API响应返回,那么这里可能是什么问题? Any help will be much appreciated.
任何帮助都感激不尽。
Don't use Page<ObjectDto>
as return type of rest methods. 不要使用
Page<ObjectDto>
作为rest方法的返回类型。 This Page is not for that. 此页面不适合该页面。 use return type as
List<ObjectDto>
使用返回类型作为
List<ObjectDto>
and in log you are printing 并在日志中您正在打印
log.info(String.valueOf(objectDtos.getTotalElements()));
so its printing properly, if you return objectDtos.getTotalElements()
in rest method then it will also work 因此它的打印正确,如果您在rest方法中返回
objectDtos.getTotalElements()
,那么它也将正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.