【发布时间】:2018-12-26 08:50:46
【问题描述】:
我的项目中有以下 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 中实现?
【问题讨论】:
-
您的页面可能有一定的大小。如果您从中删除一些元素,您最终可能会得到一个不完整的页面,客户端。你不能用你的查询来排除你想排除的元素吗?
-
你的意思是QueryDSL吗?
-
在这里,您正在调用
findAll方法,该方法不使用任何标准并选择您的所有实体。为什么不能在存储库中定义另一个方法(使用@Query)并使用所需的条件来排除某些实体? -
我正在使用 QueryDSL 谓词,因此我可以根据 queryParams 动态过滤结果。现在 queryParams 是由用户在调用 enpoint 时发送的,但我想为谓词添加一个额外的条件,但我不知道如何实现它
-
这对我来说就像一个x y problem。我建议您学习如何向谓词添加条件,而不是尝试从页面中删除元素。
标签: java spring spring-data-rest querydsl