问题
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);
}
Now I want to remove some elements of the Page based on a condition. How do I implement in Spring Data Rest?
回答1:
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, new PageRequest(page, size),modifiedAppList.getTotalElements());
来源:https://stackoverflow.com/questions/51401929/spring-data-rest-how-to-remove-an-element-from-a-page