Search code examples
javagoogle-app-enginerestgoogle-cloud-endpointsjdo

Querying with multiple id parameters in google app engine


I really wanted to get Objectify to work, but I just couldn't figure out why it didn't work in my project so I switched to JDO instead and have these problems. I'm doing a project management app, so since a person can be engaged in multiple projects and projects can have many people on them, I have made an unowned relationship between Person.java and 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;
    }
}

So, I've tried to do a query that will return all the projects a person is assigned to. This is what I have so far:

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

So far, I've only got a 404 when I supply a list of valid project ids

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

and if I just enter one (still valid) id I get a NullPointerException

503 Service Unavailable

- Show headers -

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

Can somebody see what's going on or if I've implemented the method all wrong?


Solution

  • It seems like you have already got rid of the NullPointerException using Peter's suggestion. To solve the problem of the 404 Not Found Exception, you would have to add the @Nullable annotation before the @Named annotation of your projects parameter of the API method getPersonsProjects. This will tell App Engine to treat this parameter as a query parameter that does not need a URL path.

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