Google APis Explorer没有从我的app-engine应用程序中找到我可用的ApiMethod

时间:2013-04-26 12:28:32

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

我有一个可编译的App-Engine应用程序,我在localhost(https://developers.google.com/apis-explorer/?base=http://localhost:8888/_ah/api#p/)上使用Google Apis Explorer测试方法调用 它工作正常,我可以使用Apis Explorer界面测试我的api方法,但只要我添加一个新的Apimethod

@ApiMethod(name = "users.insertrandomuserv4")
public User insertrandomuserv4() {
    User user = new User("234","myfirstname","mysecondname");
    return user;
}

我在本地重新部署我的应用程序,Apis Explorer没有显示不同api方法的列表。 为什么? (仅供参考:当我删除新方法时,它会再次起作用)

编辑: 这是我班级的其余部分(是的,我使用PersistentManager和DataStoreService,但它仅用于测试):

public class UserEndpoint {
@ApiMethod(name = "users.get")
public User get(@Named("key") String key) {
    //Key k = KeyFactory.createKey(User.class.getSimpleName(), key);
    PersistenceManager pm = getPersistenceManager();
    User user = pm.getObjectById(User.class, key);
    pm.close();
    return user;
}

@ApiMethod(name = "users.list")
public List<User> list() {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    Query query = new Query("User");
    //query.setKeysOnly();
    PreparedQuery pq = datastore.prepare(query);
    List<Entity> ls = pq.asList(FetchOptions.Builder.withLimit(500));
    List<User> flist = new ArrayList<User>();
    for (Entity user : ls) {
        User u = new User(user);
        flist.add(u);
    }
    return flist;
}



@ApiMethod(name = "users.insert")
public User insert(User user) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity ent = user.toEntity();
    datastore.put(ent);
    return user;
}

@ApiMethod(name = "users.insertrandomuserv4")
public User insertrandomuserv4() {
    User user = new User("234","myfirstname","mysecondname");
    return user;
}




@ApiMethod(name = "users.getuserswithsamehometown")
public List<User> getUsersWithSameHometown(@Named("home")String home) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query query = new Query("User");
    //query.addProjection(new PropertyProjection("firstname", User.class));
    query.setFilter(new Query.FilterPredicate("hometown",FilterOperator.EQUAL,home));
    PreparedQuery pq = datastore.prepare(query);
    List<Entity> ls = pq.asList(FetchOptions.Builder.withLimit(500));
    List<User> flist = new ArrayList<User>();
    for (Entity ent : ls) {
        User u = new User(ent);
        flist.add(u);
    }
    return flist;       
}


private static PersistenceManager getPersistenceManager() {
    return JDOHelper.getPersistenceManagerFactory("transactions-optional").getPersistenceManager();
}
}

1 个答案:

答案 0 :(得分:5)

是的,这里有路径碰撞。在我对您的代码的审核中,我认为存在两个冲突:users.getusers.getuserswithsamehometown冲突,users.insertusers.insertrandomuserv4冲突。

您使用的命名约定可以防止API客户端库中的冲突,但不会阻止用于调用API的实际路径。例如,两个.get调用都映射到GET users/{string},而.insert个调用都映射到POST users

您应该做的是为每对中的一个冲突方法添加路径参数。例如,users.getuserswithsamehometown您可以更新为:

@ApiMethod(
  name = "users.getuserswithsamehometown",
  path = "users/hometown/{home}",
  httpMethod = "GET"
)

这将为该方法提供一个不再与其他users.get方法冲突的路径。您可以为有冲突的.insert电话重复此过程,例如:

@ApiMethod(
  name = "users.insertrandomuserv4",
  path = "users/random/",
  httpMethod = "POST"
)

我已经添加了httpMethod声明以获得良好的衡量标准。当你开始修改路径的其他部分时,通常最好声明这些。