从后端API生成发现文档时出错

时间:2014-05-08 06:44:55

标签: google-cloud-endpoints

从我的../war目录(来自https://developers.google.com/appengine/docs/java/endpoints/endpoints_tool#generating_a_discovery_doc)执行此命令时:

D:\programs\eclipse-kepler\plugins\com.google.appengine.eclipse.sdkbundle_1.9.4\appengine-java-sdk-1.9.4\bin\endpoints.cmd get-discovery-doc --format=rpc com.smartinteractive.blackwoods.entity.BranchEndpoint com.smartinteractive.blackwoods.PropertyEndpoint

这是结果:

V 08, 2014 9:31:11 AM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
INFO: Successfully processed ./war\WEB-INF/appengine-web.xml
Error: com.smartinteractive.blackwoods.entity.BranchEndpoint

我不知道这可能是什么样的错误。我在这个主题(App engine RPC discovery doc)中读到了

  

在Endpoint方法中,返回值类型不能是简单类型   例如String或int。返回值必须是POJO,一个数组   或收藏品

还有什么可能是问题。

BranchEndpoint:

package com.smartinteractive.blackwoods.entity;

import com.smartinteractive.blackwoods.dao.service.BranchDataService;
import com.smartinteractive.blackwoods.persistence.EMF;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.CollectionResponse;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityManager;

@Api(name = "branchendpoint", namespace = @ApiNamespace(ownerDomain = "smartinteractive.com", ownerName = "smartinteractive.com", packagePath = "blackwoods.entity"))
public class BranchEndpoint {

    /**
     * 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
     */
    @SuppressWarnings("unchecked")
    @ApiMethod(name = "listBranch")
    public CollectionResponse<Branch> listBranch(@Nullable @Named("first") Integer first, @Nullable @Named("last") Integer last) {

        EntityManager mgr = getEntityManager();
        List<Branch> execute = null;

        try {
            if(first == null || last == null) {
                execute = new BranchDataService().findWithNamedQuery(Branch.ALL);
            }
            else {
                execute =  (List<Branch>)new BranchDataService().findAllBranches(mgr, first, last, null, null, null);
            }
        } finally {
            mgr.close();
        }

        return CollectionResponse.<Branch> builder().setItems(execute).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 = "getBranch")
    public Branch getBranch(@Named("id") Integer id) {
        EntityManager mgr = getEntityManager();
        Branch branch = null;
        try {
            branch = mgr.find(Branch.class, id);
        } finally {
            mgr.close();
        }
        return branch;
    }

    /**
     * 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 branch the entity to be inserted.
     * @return The inserted entity.
     */
    @ApiMethod(name = "insertBranch")
    public Branch insertBranch(Branch branch) {
        EntityManager mgr = getEntityManager();
        try {
            if (containsBranch(branch)) {
                throw new EntityExistsException("Object already exists");
            }
            mgr.persist(branch);
        } finally {
            mgr.close();
        }
        return branch;
    }

    /**
     * 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 branch the entity to be updated.
     * @return The updated entity.
     */
    @ApiMethod(name = "updateBranch")
    public Branch updateBranch(Branch branch) {
        EntityManager mgr = getEntityManager();
        try {
            if (!containsBranch(branch)) {
                throw new EntityNotFoundException("Object does not exist");
            }
            mgr.persist(branch);
        } finally {
            mgr.close();
        }
        return branch;
    }

    private boolean containsBranch(Branch branch) {
        EntityManager mgr = getEntityManager();
        boolean contains = true;
        try {
            Branch item = mgr.find(Branch.class, branch.getId());
            if (item == null) {
                contains = false;
            }
        } finally {
            mgr.close();
        }
        return contains;
    }

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

}

0 个答案:

没有答案