Friday, November 17, 2017

JsInterop Shared Model + REST API

I'm trying to create a shared model in my GWT 2.8.2 application that I can use both at the server and in GWT client code.

My research/Google Fu tells me that this might be possible with JsInterop but since I'm new to JsInterop I'm just not sure.
I've done some experiments but I've run into a couple of issues with the biggest being that I can't have my model accessor methods if I make the model native.  The compiler tells me that they have to be native or abstract.

What I'd like to do with my model is:
public class User {
    private double id;
    private String code;
    private String name;

    public User() {

    public User(double id, String code, String name) {
        this(); = id;
        this.code = code; = name;

    public double getId() {
        return id;

    public double setId(double id) { = id;

Then in my GWT application I'd like to do this:

    public class JsTypes {

        public static native <T> T getJsTypeObject(JavaScriptObject result)/*-{
            return result;

    //called after REST response from server
    private void onCallback(String json) {
Console.log("JSON:" + json);
JavaScriptObject result = JsonUtils.safeEval(json);
User user = JsTypes.getJsTypeObject(result);
        //do something with user

Any ideas or suggestions would be greatly appreciated.

You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment