Spring Data Rest-如何从页面中删除元素?

时间:2018-07-18 12:22:36

标签: java spring spring-data-rest querydsl

我的项目中具有以下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中实现?

1 个答案:

答案 0 :(得分:3)

您不能直接从页面中删除元素。您可以做的是,从页面中获取内容,该内容将是一个列表,然后根据您的条件从列表中删除元素,然后使用修改后的列表和大小创建一个新的页面。

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());