A few days ago, I started developing Backend for mobile applications using the Google App Engine and Google Cloud endpoints . This guide shows how endpoints are created automatically, as well as the Android client library.
So, we have our Entity:
@Entity public class Person implements IsSerializable{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Key key; private String name;
And the endpoint for this class:
@Api(name = "personendpoint") public class PersonEndpoint { @ApiMethod(name = "getPerson") public Person getPerson(@Named("id") Long id) { ...
In addition, using the created Android endpoint library (which uses the REST API), I would like to add a user interface on the server, create using the Google Web Toolkit (GWT) . But how do I manipulate the date on the server side? I see different approaches ...
Option A1: Add RPC Service to GWT
public interface PersonServiceAsync { void insertPerson(Person person, AsyncCallback<Person> callback); } @RemoteServiceRelativePath("api") public interface PersonService extends RemoteService { public Person insertPerson(Person person); } public class PersonServiceImpl extends RemoteServiceServlet implements PersonService{ public Person insertPerson(Person person) { EntityManager mgr = getEntityManager(); try { if (containsPerson(person)) { throw new EntityExistsException("Object already exists"); } mgr.persist(person); } finally { mgr.close(); } return person; }
But now my PersonServiceImpl and PersonEndpoint are doing roughly the same thing. So we did not follow DRY :) Also, this person is not allowed to have com.google.appengine.api.datastore.Key , so we would have to change our entities.
Parameter A2: The service endpoint class calls
@Override public Person insertPerson(Person person) { return new PersonEndpoint().insertPerson(person); }
Should work, but still no com.google.appengine.api.datastore.Key Enter Entity and, since Endpoints use CollectionResponse<Person> , we would have to convert this to Collection<Person> in the case of listPerson() .
Option B1: Using the Java Endpoint Client Library
We could split the GWT client from our App Engine API and use the created client endpoint libraries for Java. Therefore, we call the REST / Endpoint-API from the RemoteServiceServlet . But will it not be in two requests, even if the GWT client and endpoints are on the same server or even in the same project?
GWT Client - (RPC) โ GWT Server - (HTTP Request) โ Backup Application Server Server
Option B2: Using the JavaScript Client Endpoint Library
This may be the best approach, but will end up in massive JSNI.
So what's the best practice? I can not find sample projects using Google Cloud Endpoints AND GWT in one project :)