在谷歌应用引擎中查询多个id参数

时间:2014-10-08 20:44:13

标签: java google-app-engine rest google-cloud-endpoints jdo

我真的想让Objectify工作,但我无法弄清楚为什么它在我的项目中没有工作所以我转而使用JDO来解决这些问题。我正在做一个项目管理应用程序,所以既然一个人可以参与多个项目,项目上可以有很多人,我在Person.java和Project.java之间建立了一个无主的关系

Person.java     package com.pontuse.appendpoint;

import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Person {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Long id;
    @Persistent
    String userName;
    @Persistent
    String pass;
    //Modelling relationship with projects
    @Persistent
    List<Long> projectsInvolved;

    public Person() {
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPass() {
        return pass;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }

    public List<Long> getProjectsInvolved() {
        return projectsInvolved;
    }

    public void setProjectsInvolved(List<Long> projectsInvolved) {
        this.projectsInvolved = projectsInvolved;
    }
}

Project.java

package com.pontuse.appendpoint;

import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Project {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Long id;
    @Persistent
    Long adminId;
    @Persistent
    Date deadline;

    //Modelling relationship with people
    @Persistent
    List<Long> peopleOn;
    @Persistent
    List<Long> tasksIn;
    @Persistent
    List<Long> tasksDoing;
    @Persistent
    List<Long> tasksDone;

    public Project() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getAdminId() {
        return adminId;
    }

    public void setAdminId(Long adminId) {
        this.adminId = adminId;
    }

    public Date getDeadline() {
        return deadline;
    }

    public void setDeadline(Date deadline) {
        this.deadline = deadline;
    }

    public List<Long> getTasksIn() {
        return tasksIn;
    }

    public void setTasksIn(List<Long> tasksIn) {
        this.tasksIn = tasksIn;
    }

    public List<Long> getTasksDoing() {
        return tasksDoing;
    }

    public void setTasksDoing(List<Long> tasksDoing) {
        this.tasksDoing = tasksDoing;
    }

    public List<Long> getTasksDone() {
        return tasksDone;
    }

    public void setTasksDone(List<Long> tasksDone) {
        this.tasksDone = tasksDone;
    }

    public void setPeopleOn(List<Long> peopleOn) {
        this.peopleOn = peopleOn;
    }

    public List<Long> getPeopleOn() {
        return peopleOn;
    }
}

所以,我已经尝试进行一个查询,该查询将返回一个人被分配到的所有项目。这就是我到目前为止所做的:

PojectEndpoint.java

@ApiMethod(name = "getPersonsProjects")
    public CollectionResponse<Project> getPersonsProjects(@Named("projects") List<Long> projects){
        PersistenceManager mgr = null;
        List<Project> execute = new ArrayList<Project>();

        for(Long l: projects)
        execute.add(mgr.getObjectById(Project.class, l));

        mgr.close();
        return CollectionResponse.<Project> builder().setItems(execute).build();
    }

到目前为止,当我提供有效项目ID列表时,我只获得了404

404 Not Found

- Hide headers -

Cache-Control:  no-cache, no-store, max-age=0, must-revalidate
Content-Encoding:  gzip
Content-Length:  29
Content-Type:  text/html; charset=UTF-8
Date:  Wed, 08 Oct 2014 20:38:05 GMT
Expires:  Fri, 01 Jan 1990 00:00:00 GMT
Pragma:  no-cache
Server:  GSE

Not Found

如果我只输入一个(仍然有效)id,我会得到一个NullPointerException

503 Service Unavailable

- Show headers -

{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "java.lang.NullPointerException"
}
],
"code": 503,
"message": "java.lang.NullPointerException"
}
}

有人可以看到发生了什么,或者我是否实施了这个方法都错了?

1 个答案:

答案 0 :(得分:1)

看起来你已经使用彼得的建议摆脱了NullPointerException。要解决404 Not Found Exception的问题,您必须在API方法@Nullable的{​​{1}}参数的@Named注释之前添加projects注释。这将告诉App Engine将此参数视为不需要URL路径的查询参数。

来源:https://cloud.google.com/appengine/docs/java/endpoints/paramreturn_types#query_parameters