Spring Data Rest - 如何从页面中删除元素? [英] Spring Data Rest - How to remove an element from a Page?
本文介绍了Spring Data Rest - 如何从页面中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的项目中有以下 REST 控制器方法
I have the following REST controller method in my project
@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 中实现?
Now I want to remove some elements of the Page based on a condition. How do I implement in Spring Data Rest?
推荐答案
您不能直接从页面中删除元素.您可以做的是,从页面中获取内容,这将是一个列表,然后根据您的条件从列表中删除元素,然后使用修改后的列表和大小创建一个新页面.
You can't directly remove elements from a Page. What you can do is, get the content from the page, that will be a list and then remove the the element from the list according to your condition , then create a new Page with the modified list and size.
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());
这篇关于Spring Data Rest - 如何从页面中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文