3

我有一个编译良好的 App-Engine 应用程序,我使用本地主机上的 Google APIs Explorer 测试方法调用(https://developers.google.com/apis-explorer/?base=http://localhost:8888/_ah /api#p/)它工作正常,我可以使用 API 资源管理器界面测试我的 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();
}
}
4

1 回答 1

5

是的,这里发生了路径碰撞。根据我对您的代码的审查,我相信有两种冲突:与 .users.get碰撞users.getuserswithsamehometownusers.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 声明以获得良好的衡量标准。当您开始修改路径的其他部分时,通常最好声明这些。

于 2013-04-29T16:04:58.833 回答