GWT and Google Cloud Endpoints

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 :)

+47
google-app-engine gwt-rpc google-cloud-endpoints
May 28 '13 at 7:51
source share
2 answers

Good old DTO dilemma. There is no right or wrong, just what is enough for you.

Repeating yourself can be good. Right now, you are exposing your data model to your endpoint, which means that any change to your objects will affect users of mobile applications. Let's say you rename the attribute on the server side -> every client that has not updated the application is omitted.

Security is also a problem: if your user object has the "email" property, serializing it through GWT RPC will make your user email almost accessible to any javascript debugger.

Is this really what you want?

Do not get me wrong, I'm not a fan of these โ€œmonstersโ€ of the moon layers, where 80% of the code seems to be made to convert objects to other objects with almost the same properties.

I think that the right solution is between them: the presence of a โ€œclientโ€ model (DTO) made of serializable POJOs (without data, ORM, JAXB, any annotation) that you look through both the GWT RPC and the client's Endpoints. Your GWT servlet implementation and the endpoint server will call the same service, which converts your client model into objects and processes / stores them.

That way, you can reuse your code, still keep it simple, have a unified interface across your APIs and allow your internal plumber to evolve without changing the client interfaces.

+9
Mar 21 '14 at 13:27
source share

Maybe I donโ€™t understand anything. But everything seems very easy to me.

  • GWT (client!) Is not on the server. It is compiled by Javascript executed in a client browser.

  • The Google plugin generates Javascript client code that invokes the endpoint with the appropriate JSON.

  • The above code can be called from GWT.

Voila?

0
Jan 08 '15 at 9:42
source share



All Articles