问题
How can I pass more than one Entity from a client to a Google Cloud Endpoint?
For example, passing a single Entity is easily done in an Endpoint api source file in the server:
public class SomeEndpoint {
...
@ApiMethod(...)
public MyEntity someMethod(MyEntity someEntity) {
...
}
...
}
then in a client I could easily call
endpoint.someMethod(someEntity).execute()
But, what if I want to pass two entities to an endpoint?, like this:
@ApiMethod(...)
public MyEntity otherMethod(MyEntity someEntity, MyEntity someOtherEntity) {
...
}
this doesn't work, GPE only generates an endpoint library with a single MyEntity argument.
Is it possible to pass multiple Entity arguments?
Thanks.
回答1:
You can't send multiple entity types in the body of your request. You'll need to create a wrapper entity that contains those two entities, e.g.:
class MyWrapperEntity {
MyEntity someEntity;
MyOtherEntity someOtherEntity;
// ...
}
However, that's not what your example shows (the entities are the same type). Use a List<MyEntity>
or Map<String, MyEntity>
inside of a collection entity instead, e.g.:
class MyEntityCollection {
List<MyEntity> items;
// ...
}
回答2:
Use the 'named' annotation...
@ApiMethod(name = "sendStuff")
public void sendStuff( @Named("clientId") String clientId, @Named("stuff") String stuff )
And for android, the client code would look like this
SendStuff sl = service.sendStuff( clientId, stuff );
来源:https://stackoverflow.com/questions/15450472/multiple-entity-arguments-in-google-cloud-endpoints