//
archives

GWT

This category contains 4 posts

Origin http://127.0.0.1:8888 is not allowed by Access-Control-Allow-Origin – GWT Debugging

During GWT debugging, if you are making a RequestBuilder on a different domain without using JSONP, you may have this problem.
To fix it you must configure Header in order to accept requests from other domains. In Apache HTTP Server you can set them in the VirtualHost configuration of bar.com using the directive Header set.

 
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "X-Requested-With"

Or better filtering by your origin – see also https://developer.mozilla.org/En/HTTP_access_control#Access-Control-Allow-Origin


Header set Access-Control-Allow-Origin "http://foo.com"
Header set Access-Control-Allow-Headers "X-Requested-With"

GWT and JSON error in eval – JavaScriptException: (SyntaxError): Unexpected token :

In GWT 2.x you can do a JSON-P request for CORS using JsonpRequestBuilder. That’s is really useful! But if you have a service that don’t support JSON-P or you want to use JSON with Access-Control-Allow-Origin header for CORS you have to use JSNI eval in GWT.

public void getJson(String url, final AsyncCallback asyncCallback) {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
asyncCallback.onFailure(exception);
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
String json = response.getText();
asyncCallback.onSuccess(evalJson(json));
} else {
asyncCallback.onFailure(new Throwable());
}
}
});
} catch (RequestException e) {
}
}


private final native MyJson evalJson(String json) /*-{

return eval(json);
}-*/;

throws an exception


com.google.gwt.core.client.JavaScriptException: (SyntaxError): Unexpected token :
arguments: :
type: unexpected_token
stack: SyntaxError: Unexpected token :
at eval (native)

to solve this issue simply assign a variable to your json string into the eval

private final native MyJson evalJson(String json) /*-{
eval('var res = ' + json);
return res;
}-*/;

that’s it :-)

GWT Serialization Error – was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded

GWT is a very useful programming framework but sometime it’s very difficult to find out how to solve some problems.

One of the is related to GWT-RPC Serialization. GWT-RPC is the canonical way to make AJAX call from GWT Client to GWT Server passing Serializable beans (more details on GWT web site - http://code.google.com/webtoolkit/doc/latest/tutorial/RPC.html).

To pass a bean you have to fulfill the following requirements (from GWT site):

  1. It implements either Java Serializable or GWT IsSerializable interface, either directly, or because it derives from a superclass that does.
  2. Its non-final, non-transient instance fields are themselves serializable
  3. It has a default (zero argument) constructor with any access modifier (e.g. private Foo(){} will work)

Even if you fulfill these requirements may happen that GWT compiler say:

<class name> was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = <class instance>@<id>

The problem may have different causes. Here his a complete check list to use for solving the problem:

  1. Verify that the class has a default constructor (without arguments)
  2. Verify that the class implements Serializable or IsSerializable or implements an Interface that extends Serializable or extends a class that implement Serializable
  3. Verify that the class is in a client.* package or …
  4. Verify, if the class is not in client.* package, that is compiled in your GWT xml module definition. By default <source path=”client” /> is present. If your class is in another package you have to add it to source. For example if your class is under domain.* you should add it to xml as <source path=”domain” /> . Be aware that the class cannot belong to server package! More details on GWT page: http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideModuleXml
  5. If you are including the class from another GWT project you have to add the inherits to your xml module definition. For example if your class Foo is in the package com.dummy.domain you have to add <inherits name=”com.dummy.domain.Foo”/> to the module definition. More details here: http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideInheritingModules
  6. If you are including the class from another GWT project released as a jar verify that the jar contains also the source code because GWT recompile also the Java source for the classes passed to the Client.

I hope that this help. Please let me know if I miss some check or I wrote something no more valid. This check list is updated for GWT 2.2.

How to setup multiple database connections with JPA, Hibernate, Spring, Lucene and GWT


I’m working on Open Source project gwt-spring-jpa-lucene. The aim is to explain with concrete examples how to create a GWT project that manages more than one databases at the same time using Spring and JPA.

Actually I’ve posted the source code and the Wiki page for the main configuration.

Please have a look! If you need help don’t hesitate to contact me!

Twitter Updates

Follow

Get every new post delivered to your Inbox.

Join 67 other followers