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

Vin*_*eFR 3 google-app-engine google-cloud-endpoints

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

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

我在本地重新部署我的应用程序,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();
}
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*oet 5

是的,这里有路径碰撞.从我对你的代码的评论,我相信有两个碰撞:users.get碰撞users.getuserswithsamehometownusers.insert碰撞users.insertrandomuserv4.

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

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

@ApiMethod(
  name = "users.getuserswithsamehometown",
  path = "users/hometown/{home}",
  httpMethod = "GET"
)
Run Code Online (Sandbox Code Playgroud)

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

@ApiMethod(
  name = "users.insertrandomuserv4",
  path = "users/random/",
  httpMethod = "POST"
)
Run Code Online (Sandbox Code Playgroud)

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