Spring Data Rest - How to remove an element from a Page?

我是研究僧i 提交于 2020-02-05 07:19:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!