Endpoint GET语句出现问题

时间:2013-05-30 03:52:27

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

使用Google App Engine,我为Client课程创建了一个端点。当我使用简单的GET语句访问它时,它会出错。即使我只插入了简单数据,也会发生这种情况。

我在最后放了一些我的课程的骨架代码。我猜我需要对private List<Assessment> assessment类中的行Client做一些奇特的注释,但我不知道该怎么做。

有没有人知道如何解决这个问题,如果可能的话,我想知道为什么这个错误与设置有关,因为评估字段中没有数据。

插入数据

{
  "firstName" : "Jon",
  "lastName" : "Doe"
}

插入后,执行此简单语句以选择所有 firstName lastName 字段。

GET http://localhost:8888/_ah/api/clientendpoint/v1/client?fields=items(firstName%2Cid%2ClastName)

这是我收到错误的地方

错误

  

com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException:您刚刚尝试访问字段“评估”,但是当您分离对象时,此字段未分离。要么不访问此字段,要么在分离对象时将其分离。 (通过参考链:com.google.api.server.spi.response.CollectionResponse [\“items \”] - &gt; com.google.appengine.datanucleus.query.StreamingQueryResult [0] - &gt; com.my.app .client.Client [\ “评估\”])

更新:我通过Eclipse Google API插件生成了端点。我把它放在最后,这对应于GET语句。

我进一步追踪,可能只是我对如何创建返回的对象缺乏了解。

错误似乎在

  

com.my.app.client.Client.jdoGetassessment(Client.java)   com.my.app.client.Client.getAssessment(Client.java:247)

但我的代码中不存在jdoGetAssessment(),所以我猜这是在Google App Engine中生成的。 getAssessment()由。{/ p>定义

public List<Assessment> getAssessment() {
     return assessment;
}

这似乎触发了错误消息,但我仍然不明白为什么在没有数据的情况下触发它。


客户端

@Entity
public class Client {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key id;
    private String firstName;
    private String lastName;

    @ElementCollection
    private List<Assessment> assessment;

    public Key getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<Assessment> getAssessment() {
        return assessment;
    }

    public void setAssessment(List<Assessment> assessment) {
        this.assessment = assessment;
    }
}

评估

@Embeddable
public class Assessment {
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

生成ClientEndpoint.java

@Api(name = "clientendpoint", namespace = @ApiNamespace(ownerDomain = "myapp.com", ownerName = "myapp.com", packagePath = "mypackage.client"))
public class ClientEndpoint {

    /**
     * This method lists all the entities inserted in datastore.
     * It uses HTTP GET method and paging support.
     *`
     * @return A CollectionResponse class containing the list of all entities
     * persisted and a cursor to the next page.
     */
    @SuppressWarnings({ "unchecked", "unused" })
    @ApiMethod(name = "listClient")
    public CollectionResponse<Client> listClient(
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {

        EntityManager mgr = null;
        Cursor cursor = null;
        List<Client> execute = null;

        try {
            mgr = getEntityManager();
            Query query = mgr.createQuery("select from Client as Client");
            if (cursorString != null && cursorString != "") {
                cursor = Cursor.fromWebSafeString(cursorString);
                query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
            }

            if (limit != null) {
                query.setFirstResult(0);
                query.setMaxResults(limit);
            }

            execute = (List<Client>) query.getResultList();
            cursor = JPACursorHelper.getCursor(execute);
            if (cursor != null)
                cursorString = cursor.toWebSafeString();

            // Tight loop for fetching all entities from datastore and accomodate
            // for lazy fetch.
            for (Client obj : execute)
                ;
        } finally {
            mgr.close();
        }

        return CollectionResponse.<Client> builder().setItems(execute)
                .setNextPageToken(cursorString).build();
    }

    /**
     * This method gets the entity having primary key id. It uses HTTP GET method.
     *
     * @param id the primary key of the java bean.
     * @return The entity with primary key id.
     */
    @ApiMethod(name = "getClient")
    public Client getClient(@Named("id") Long id) {
        EntityManager mgr = getEntityManager();
        Client client = null;
        try {
            client = mgr.find(Client.class, id);
        } finally {
            mgr.close();
        }
        return client;
    }

    /**
     * This inserts a new entity into App Engine datastore. If the entity already
     * exists in the datastore, an exception is thrown.
     * It uses HTTP POST method.
     *
     * @param client the entity to be inserted.
     * @return The inserted entity.
     */
    @ApiMethod(name = "insertClient")
    public Client insertClient(Client client) {
        EntityManager mgr = getEntityManager();
        try {
            if (containsClient(client)) {
                throw new EntityExistsException("Object already exists");
            }
            mgr.persist(client);
        } finally {
            mgr.close();
        }
        return client;
    }

    /**
     * This method is used for updating an existing entity. If the entity does not
     * exist in the datastore, an exception is thrown.
     * It uses HTTP PUT method.
     *
     * @param client the entity to be updated.
     * @return The updated entity.
     */
    @ApiMethod(name = "updateClient")
    public Client updateClient(Client client) {
        EntityManager mgr = getEntityManager();
        try {
            if (!containsClient(client)) {
                throw new EntityNotFoundException("Object does not exist");
            }
            mgr.persist(client);
        } finally {
            mgr.close();
        }
        return client;
    }

    /**
     * This method removes the entity with primary key id.
     * It uses HTTP DELETE method.
     *
     * @param id the primary key of the entity to be deleted.
     * @return The deleted entity.
     */
    @ApiMethod(name = "removeClient")
    public Client removeClient(@Named("id") Long id) {
        EntityManager mgr = getEntityManager();
        Client client = null;
        try {
            client = mgr.find(Client.class, id);
            mgr.remove(client);
        } finally {
            mgr.close();
        }
        return client;
    }

    private boolean containsClient(Client client) {
        if (client.getId() == null)
            return false;

        EntityManager mgr = getEntityManager();
        boolean contains = true;
        try {

            Client item = mgr.find(Client.class, client.getId());
            if (item == null) {
                contains = false;
            }
        } finally {
            mgr.close();
        }
        return contains;
    }

    private static EntityManager getEntityManager() {
        return EMF.get().createEntityManager();
    }

}

1 个答案:

答案 0 :(得分:3)

问题可能是JDO延迟加载 - 您正在请求对象,而JDO在您尝试使用它们之前不会获取它们的属性,此时与数据存储区的连接已经消失。您可以在自动生成的示例中看到一个代码示例来阻止延迟加载:

// Tight loop for fetching all entities from datastore and accomodate
// for lazy fetch.
for (Client obj : execute)
  ;

这会在返回之前加载列表中的所有Client个对象。但是,List上的Client属性也会延迟加载,因此您需要为每个Client对象的assessment属性重复此过程。

编辑:删除了使用FetchType.EAGER的建议,因为它在App Engine上不起作用。