java - 在Google App Engine中使用多个id参数进行查询

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

我真的很想让 Objectify 工作,但我就是不明白为什么它在我的项目中不起作用,所以我改用 JDO 并遇到了这些问题。我正在做一个项目管理应用程序,因此由于一个人可以参与多个项目,并且项目可以有很多人,所以我在 Person.java 和 Project.java 之间建立了无主关系

Person.java 包 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"
}
}

有人可以看看发生了什么或者我是否完全错误地实现了该方法?

最佳答案

看来您已经按照 Peter 的建议摆脱了 NullPointerException 。要解决 404 Not Found 异常问题,您必须在 API 方法 getPersonsProjectsprojects 参数的 @Named 注释之前添加 @Nullable 注释。这将告诉 App Engine 将此参数视为不需要 URL 路径的查询参数。

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

关于java - 在Google App Engine中使用多个id参数进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26266102/

相关文章:

java - HTTP 406 的 Spring 返回 JSON(NOT_ACCEPTABLE)

java - 如何修复这个冒泡排序程序?

javascript - 驱动器API : DeadlineExceededException when updating a file

RESTful 子类型资源

java - Retrofit 需要太多时间才能得到响应

ruby-on-rails - 部署 ruby​​ api 谷歌云

java - 如何通过 PHP 代码运行 shell 命令?

Java:如何将 "JOptionPane.showInputDialog"中的选择返回为 "int"而不是 "Object"

java - 使用 SOAPHandler 获取 SOAP 响应的请求

java - 图片从 Internet 到 AppEngine BlobStore (Java)