首页 > 解决方案 > 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 中实现?

标签: javaspringspring-data-restquerydsl

解决方案


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

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

推荐阅读