I am working on a GWT project where ASP.NET is my only option on the server. I have found that, while it requires a little extra work, JavaScript Overlay types can make it easy. It doesn't matter what technology your server is using, so long as it can respond with JSON serialized data. For instance, if you have a Contact class in C#:
public class Contact
{
public int Id { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
}
Design your server so that it returns this serialized as JSON. I use ASP.NET MVC for this because it requires so little work. Here's a very simple example where we'll assume that Contact has a static method that, given an id, will return a Contact instance:
public class ContactController : Controller
{
public ActionResult GetContact (int id)
{
return Json(Contact.GetById(id), JsonRequestBehavior.AllowGet);
}
}
Now, in your GWT application, create a JavaScript overlay type for your Contact:
import com.google.gwt.core.client.JavaScriptObject;
public class Contact extends JavaScriptObject {
protected Contact() { }
public final native int getContactId() /*-{ return this.Id; }-*/;
public final native String getLastName() /*-{ return this.LastName; }-*/;
public final native String getFirstName() /*-{ return this.FirstName; }-*/;
public final native String getEmail() /*-{ return this.Email; }-*/;
public static final native Contact createFromJson(String json) /*-{
return eval('(' + json + ')');
}-*/;
}
Next, in your GWT project, use an HTTP request to communicate with the server:
public class ContactLoader {
private int id;
public ContactLoader(int id) {
this.id = id;
}
public void beginLoad() {
String url = "/YourService/GetContact/?id=" + id;
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
@SuppressWarnings("unused")
builder.sendRequest(null, new RequestCallback() {
@Override
public void onResponseReceived(Request req, Response resp) {
Contact contact = Contact.createFromJson(req.getText());
// do something with your contact. In my project, I
// fire an event here for which the contact is the payload
}
@Override
public void onError(Request request, Throwable exception) {
// handle your error
}
catch (Exception exception) {
// handle your error
}
}
}
It is also possible to use generic types with overlay types. For example, I never return a bare object, I always use a generic container for transport so that I can easily handle error reporting on the client side.
C#:
public class ServerResponse
{
public T Payload { get; set; }
public bool Success { get; set; }
public String Message { get; set; }
}
Java:
public class ServerResponse
extends JavaScriptObject {
protected ServerResponse() { }
public final native T getPayload() /*-{ return this.Payload; }-*/;
public final native boolean getSuccess() /*-{ return this.Success; }-*/;
public final native String getMessage() /*-{ return this.Message; }-*/;
}
This lets me return my Contact, an array of Contacts or whatever. It also helps in reducing duplication in your data loading logic.
This is an overly simple example, but hopefully it is enough to clear a path for anyone needing help with a non-Java back-end. Another resource that I found helpful that discusses overlay types is "JSON Parsing with JavaScript Overlay Types in GWT" by Matt Raible.