Spring Data错误地形成了请求

huangapple go评论68阅读模式
英文:

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&lt;Task&gt; getAllTasksByProjectId(@RequestParam(value = &quot;page&quot;, defaultValue = &quot;1&quot;) Integer page,
                                         @RequestParam(required = false) MultiValueMap&lt;String, String&gt; params) {
    TaskFilter taskFilter = new TaskFilter(params);
    return taskService.findAllTasksByProject(taskFilter.getSpec(), page - 1, 5).getContent();
}

Service:

public Page&lt;Task&gt; findAllTasksByProject(Specification&lt;Task&gt; spec, int page, int size) {
    return taskRepository.findAll(spec, PageRequest.of(page, size));
}

Repository:

@Repository
public interface TaskRepository extends JpaRepository&lt;Task, Long&gt;, JpaSpecificationExecutor&lt;Task&gt; {
}

Filter:

 public TaskFilter(MultiValueMap&lt;String, String&gt; params) {
        spec = Specification.where(null);
        if (params.containsKey(&quot;is_archived&quot;)) {
            spec.and(TaskSpecifications.isArchived(Boolean.parseBoolean(params.getFirst(&quot;is_archived&quot;))));
            System.out.println(params.getFirst(&quot;is_archived&quot;));
        }
        if (params.containsKey(&quot;project&quot;)) {
            spec = spec.and(TaskSpecifications.projectEqual(Long.parseLong(params.getFirst(&quot;project&quot;))));
        }
    }

And Specification:

public static Specification&lt;Task&gt; projectEqual(Long projectId) {
    return (Specification&lt;Task&gt;) (root, criteriaQuery, criteriaBuilder) -&gt; criteriaBuilder.equal(root.get(&quot;project&quot;), projectId);
}
public static Specification&lt;Task&gt; isArchived(Boolean isArchived) {
    return (Specification&lt;Task&gt;) (root, criteriaQuery, criteriaBuilder) -&gt; criteriaBuilder.equal(root.get(&quot;isArchived&quot;), 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(&quot;is_archived&quot;))));

Every time I create new specification, instead of add in old specification.

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

huangapple
  • 本文由 发表于 2020年9月15日 01:18:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63889018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定