Java SpringDataREST-如何从页面中删除元素?
我的项目中有以下REST控制器方法Java SpringDataREST-如何从页面中删除元素?,java,spring,spring-data-rest,querydsl,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,
@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数据Rest中实现?您不能直接从页面中删除元素。您可以做的是,从页面中获取内容,这将是一个列表,然后根据您的条件从列表中删除元素,然后使用修改后的列表和大小创建一个新页面
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());
不能直接从页面中删除元素。您可以做的是,从页面中获取内容,这将是一个列表,然后根据您的条件从列表中删除元素,然后使用修改后的列表和大小创建一个新页面
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());
您的页面可能有一定的大小。如果您从中删除了一些元素,您可能会得到一个未完成的页面,即客户端。您不能使用查询排除要排除的元素吗?您的意思是QueryDSL吗?在这里,您正在调用一个不使用任何条件并选择所有实体的findAll方法。为什么不能在存储库中使用@Query定义另一个方法,并使用所需的标准排除某些实体?我使用QueryDSL谓词,以便根据queryParams动态筛选结果。现在,queryParams由用户在调用enpoint时发送,但我想在谓词中添加一个额外的条件,但我不确定如何实现它。在我看来,这就像一个简单的例子。我建议您学习如何向谓词添加条件,而不是尝试从页面中删除元素。您的页面可能有一定的大小。如果您从中删除了一些元素,您可能会得到一个未完成的页面,即客户端。您不能使用查询排除要排除的元素吗?您的意思是QueryDSL吗?在这里,您正在调用一个不使用任何条件并选择所有实体的findAll方法。为什么不能在存储库中使用@Query定义另一个方法,并使用所需的标准排除某些实体?我使用QueryDSL谓词,以便根据queryParams动态筛选结果。现在,queryParams由用户在调用enpoint时发送,但我想在谓词中添加一个额外的条件,但我不确定如何实现它。在我看来,这就像一个简单的例子。我建议您学习如何向谓词添加条件,而不是尝试从页面中删除元素。谢谢您的回复。我一定会查的out@Charlie也许会接受答案;谢谢你的回复。我一定会查的out@Charlie也许会接受答案;