Sunday, January 5, 2014

GAE endpoints: do not use @Named("key") as a parameter of @ApiMethod

Suppose we have a class EntityEndpoint in Android Studio project called Project-AppEngine.  We define a method to retrieve an entity by its key (represented as a String):
@ApiMethod(name = "getEntity”)
public MyEntity getEntity(
        User user,
        @Named("key") String key
) throws OAuthRequestException {
   // ...
}
Then we generate client libraries:
  • Tools -> Google Cloud Tools -> Generate Client Libraries
Remember, we need to do this twice as described at the end of Minimalistic GAE Endpoints in Android Studio post. 
Then, in Project-endpoints/src/endpoint-src/java/com/example/package/entityendpoint/Entityendpoint.java, we'll have a problem.  The method setKey(String) will be defined twice:
  • The first method is for setting you API key
  • The second method is for setting the key parameter we defined earlier.
To avoid this problem, we change our method definition to something like this
@ApiMethod(name = "getEntity”)
public MyEntity getEntity(
        User user,
        @Named(“id”) String key
) throws OAuthRequestException {
   // ...
}
or like this
@ApiMethod(name = "getEntity”)
public MyEntity getEntity(
        User user,
        @Named(“id”) String id
) throws OAuthRequestException {
   // ...
}
Then the second generated client method will change its name to setId(String).
It would be useful if this was mentioned in the official documentation.

No comments:

Post a Comment