英文:
Spring-data incorrectly forms the request
问题
我有一个名为tasks的表:
创建表tasks
(
id bigserial,
title varchar(25) not null,
leader_id bigint not null,
project_id bigint not null,
deadline date not null,
primary key (id),
foreign key (leader_id) references users (id),
foreign key (project_id) references projects (id),
is_archived BOOLEAN default FALSE
);
任务的REST控制器:
@GetMapping
public List<Task> getAllTasksByProjectId(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(required = false) MultiValueMap<String, String> params) {
TaskFilter taskFilter = new TaskFilter(params);
return taskService.findAllTasksByProject(taskFilter.getSpec(), page - 1, 5).getContent();
}
服务:
public Page<Task> findAllTasksByProject(Specification<Task> spec, int page, int size) {
return taskRepository.findAll(spec, PageRequest.of(page, size));
}
仓库:
@Repository
public interface TaskRepository extends JpaRepository<Task, Long>, JpaSpecificationExecutor<Task> {
}
过滤器:
public TaskFilter(MultiValueMap<String, String> params) {
spec = Specification.where(null);
if (params.containsKey("is_archived")) {
spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst("is_archived"))));
System.out.println(params.getFirst("is_archived"));
}
if (params.containsKey("project")) {
spec = spec.and(TaskSpecifications.projectEqual(Long.parseLong(params.getFirst("project"))));
}
}
以及规范(Specification):
public static Specification<Task> projectEqual(Long projectId) {
return (Specification<Task>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("project"), projectId);
}
public static Specification<Task> isArchived(Boolean isArchived) {
return (Specification<Task>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("isArchived"), isArchived);
}
所以,我想获取所有is_archived为true或false的任务。但是我获取到了所有的任务。
例如,发送到以下地址的GET请求:
> localhost:8189/tm/api/v1/tasks?page=1&is_archived=false
会返回is_archived为true和false的任务。可能是什么问题?
英文:
I have table tasks:
create table tasks
(
id bigserial,
title varchar(25) not null,
leader_id bigint not null,
project_id bigint not null,
deadline date not null,
primary key (id),
foreign key (leader_id) references users (id),
foreign key (project_id) references projects (id),
is_archived BOOLEAN default FALSE
);
Task rest controller:
@GetMapping
public List<Task> getAllTasksByProjectId(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(required = false) MultiValueMap<String, String> params) {
TaskFilter taskFilter = new TaskFilter(params);
return taskService.findAllTasksByProject(taskFilter.getSpec(), page - 1, 5).getContent();
}
Service:
public Page<Task> findAllTasksByProject(Specification<Task> spec, int page, int size) {
return taskRepository.findAll(spec, PageRequest.of(page, size));
}
Repository:
@Repository
public interface TaskRepository extends JpaRepository<Task, Long>, JpaSpecificationExecutor<Task> {
}
Filter:
public TaskFilter(MultiValueMap<String, String> params) {
spec = Specification.where(null);
if (params.containsKey("is_archived")) {
spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst("is_archived"))));
System.out.println(params.getFirst("is_archived"));
}
if (params.containsKey("project")) {
spec = spec.and(TaskSpecifications.projectEqual(Long.parseLong(params.getFirst("project"))));
}
}
And Specification:
public static Specification<Task> projectEqual(Long projectId) {
return (Specification<Task>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("project"), projectId);
}
public static Specification<Task> isArchived(Boolean isArchived) {
return (Specification<Task>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("isArchived"), isArchived);
}
So I want to get all tasks that is_archived = true or false. But I get all tasks.
For exmaple get request to
> localhost:8189/tm/api/v1/tasks?page=1&is_archived=false
returns tasks with is_archived = true and false.
What problem it can be?
答案1
得分: 0
我在这一行中有错误:
spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst("is_archived"))));
每次我创建新的规范时,而不是添加到旧的规范中。
spec = spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst("is_archived"))));
英文:
I have mistake in this line:
spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst("is_archived"))));
Every time I create new specification, instead of add in old specification.
spec = spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst("is_archived"))));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论