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

Cha*_*lie 1 java spring querydsl 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);

}
Run Code Online (Sandbox Code Playgroud)

现在我想根据条件删除页面的一些元素。如何在 Spring Data Rest 中实现?

RKA*_*RKA 8

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

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());
Run Code Online (Sandbox Code Playgroud)