java - Spring Data Rest - 如何从页面中删除元素?
问题描述
我的项目中有以下 REST 控制器方法
@RequestMapping(method = GET, value = "applications", produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody
ResponseEntity<?> getApplications(@QuerydslPredicate(root = Application.class) Predicate predicate,
PersistentEntityResourceAssembler resourceAssembler, Pageable page) {
Page<ApplicationProjection> applications = appRepo.findAll(predicate, page).
map(item -> projectionFactory.createProjection(ApplicationProjection.class, item));
return new ResponseEntity<>(pagedResourcesAssembler.toResource(applications), HttpStatus.OK);
}
现在我想根据条件删除页面的一些元素。如何在 Spring Data Rest 中实现?
解决方案
您不能直接从页面中删除元素。您可以做的是,从页面中获取内容,这将是一个列表,然后根据您的条件从列表中删除元素,然后使用修改后的列表和大小创建一个新页面。
Page<ApplicationProjection> applications = appRepo.findAll(predicate, page).
map(item -> projectionFactory.createProjection(ApplicationProjection.class, item));
List<ApplicationProjection> appList = applications.getContent();
// logic to remove the elements as per your condition modifiedAppList
// create a new Page with the modified list and size
Page<ApplicationProjection> newApplicationsPage = new PageImpl<>(modifiedAppList, PageRequest.of(pageNo, pageSize),modifiedAppList.size());
推荐阅读
- javascript - Chartist:删除所有网格线但显示轴
- python - 如何处理从文本到 CSV 的数据
- spring - 如何从 List 元素中获取 Spring 表单下拉菜单的项目?
- java - 过滤器中的请求和响应是否与 servlet 中的相同?
- jooq - jOOQ 查询 OrderBy 作为字符串
- c++ - Visual Studio 2019 和 googletest:链接器错误
- javascript - 如何将 instagram 用户个人资料添加到基本范围?
- javascript - 处理手机号码中的连续号码
- twitter-bootstrap - Bootstrap 4轮播状态转换问题
- python - 弹出列表的所有元素