【问题标题】:Querying with multiple id parameters in google app engine在谷歌应用引擎中使用多个 id 参数进行查询
【发布时间】:2014-10-08 20:44:13
【问题描述】:

我真的很想让 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"
}
}

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

【问题讨论】:

  • 您是否可以在端点方法中放置一个try catch,然后记录异常和堆栈跟踪,以查看是哪一行导致了问题。
  • 如果你还想在 Objectify 上再试一次,试试这个 Guice/Jersey/Objectify/Endpoint 样板应用程序github.com/omerio/appstart
  • NullPointerException - 可能是因为你设置了PersistenceManager mgr = null 然后调用mgr.getObjectById(..) ??
  • 好的,我设置了持久性管理器,如果我提供一个包含一个值的列表,我现在可以让它返回一个项目。两个值,它仍然返回 404。
  • 声明很明显:你不需要到处都是@Persistent。大多数这些类型默认为持久(使代码难以阅读)。不知道你的“查询”是什么。我看不到 pm.newQuery。如果您的意思是 pm.getObjectById 那么该方法返回什么?

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


【解决方案1】:

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

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

【讨论】:

  • 谢谢!您让我免于使用单独的 api 调用来获取每个元素,这会对性能造成毁灭性影响!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多