微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

弹簧数据错误地形成了请求

如何解决弹簧数据错误地形成了请求

我有表格任务:

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

任务休息控制器:

@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"))));
        }
    }

和规格:

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,criteriaBuilder) -> criteriaBuilder.equal(root.get("isArchived"),isArchived);
}

所以我想获取is_archived = true或false的所有任务。但是我得到了所有任务。 要获得例如请求,请

localhost:8189 / tm / api / v1 / tasks?page = 1&is_archived = false

返回is_archived = true和false的任务。 可能是什么问题?

解决方法

我在这一行有误:

spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst("is_archived"))));

每次创建新规范,而不是添加旧规范。

spec = spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst("is_archived"))));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。