On Friday, September 29, 2017 at 11:09:42 AM UTC+2, Jürgen Beringer wrote:
Hey,currently I tried the 2. time to move our application from gwt 2.7 to gwt 2.8, but I have big problems with the changes of JsInterop.
The application contains a serverside java part, running in a jetty and the client part, build with gwt, so I can reuse many software on server and client side. (It is the main part of the website www.spreadshirt.com )
To use also the same data classes (containing only fields with getter and setter, no other methods) on both sides, I used JsInterop and it works very well with gwt 2.7 and exchanged the data as json.Example Data class:
@JsType
public class MyObject {
private String id;
@JsProperty
public String getId() {
return id;
}
@JsProperty
public void setId(String id) {
this.id = id;
}
}The class can be used on server side like any normal java class with any REST Framework. On gwt side I was able to convert a transmitted json to the class with:
MyObject myObject = getJsTypeObject(JsonUtils.safeEval (jsonString));
//use getter and setter to access fields
public static native <T> T getJsTypeObject(JavaScriptObject result)/*-{
return result;
}-*/;And to create Objects on gwt side and send them to the server as json:
MyObject myObject = new MyObject();
myObject.setId("1");
String jsonString = stringify(myObject);
public static final native String stringify(Object result) /*-{
return JSON.stringify(result);
}-*/;I also solved the problem of List and Map with an own Interface, which is an ArrayList/HashMap on serverside and an own implemented native List/Map on gwt side.
The application has more than 100 such Objects which are heavily used on server and client side in many methods which are also be used on both sides. So implementing them twice for server and client side would be a big overhead.
Now is my question, how can I convert this gwt 2.7 code to gwt 2.8 (mainly to be able to use Java8 syntax in future). I tried multiple variants, but it seems for me, JsInterop in gwt 2.8 is not planned for such usecases which were possible in gwt 2.7 withou any problems.
I can either use a native Javascript Object in gwt (native=true) or a in GWT created Object in Javascript. But I see no solution to do both with the same Type.
I get 2 Problems
- Casting issues: I can't use native=true because I also want to create such Objects on gwt side, so on every assignment (jsonString to typed variable) I get a cast exception and at least in superdev mode I can't deactivate the cast exceptions
If you never do "instanceof" (or expect cast exceptions) on client-side, you could probably use isNative=true,namespace=GLOBAL,name="Object"; that way, all your objects are plain old JS objects (no specific class/constructor is generated in JS), which is actually exactly what you'd expect from JSON.parse().
- field name problems: The jsonString of the last example is {"id_g_$3":"1"} and not {"id":"1"} because the JsProperty for getter and setter works only fine with native objects. If I add JsProperty to the field itself, I can't do have JsProperty for the getter and setter, so I have to add JsIgnore to them. But then when I work with native Objects I can't use the getter and setter on gwt side. I would need JsProperty on getter, setter and the field itself, but this is also not allowed.
The "id_g_$3" is probably because you don't -generateJsInteropExports; contrary to 2.7, in 2.8, the "don't obfuscate @JsProperty/@JsMethod names" only applies when you -generateJsInteropExports.
But if you switch to isNative=true,namespace=GLOBAL,name="Object", you don't even need it.
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 google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment