GWT (Client) = How to convert an object to JSON and send it to the server?

I know that GWT has good RPC support. But for various purposes I need to build this myself:

1.) How to convert a Bean (on the client side), for example:

class MyPerson { String name; String getName(); void setName(String name); //.. } 

from gwt to json string? (Ideally use only those libraries that are officially included in GWT / Google).

2.) Secondly, how can I send this generated JSON string from the client side to any server using any GWT Client Logik. (Ideally use only those libraries that are officially included in GWT / Google).

I searched a lot, but the examples never show how to send data, but only to receive JSON data.

Many thanks!!! Jens

+8
java gwt
source share
4 answers

There's a great class called AutoBeanFactory that GWT will create for you, no third-party libraries are required. See http://google-web-toolkit.googlecode.com/svn-history/r9219/javadoc/2.1/com/google/gwt/editor/client/AutoBeanFactory.html

Once you have AutoBeanFactory, you can use it as follows:

creating JSON from an object of type SimpleInterface

 AutoBean<SimpleInterface> bean = beanFactory.create(SimpleInterface.class, simpleInterfaceInstance); String requestData = AutoBeanCodex.encode(bean).getPayload(); useRequestBuilderToSendRequestWhereverYouWant(requestData); 

parsing JSON from an object of type SimpleInterface

 SimpleInterface simpleInterfaceInstance = AutoBeanCodex.decode(beanFactory, SimpleInterface.class, responseText).as(); 

You can use RequestBuilder to send these requests without GWT-RPC or RF material.

+9
source share

I recommend that you use RestyGWT , which makes JSON services work the same as GWT RPC services.

+7
source share

Take a look at the GWT AutoBean framework, which you can use to create and retrieve useful JSON data. The RequestBuilder type can be used to send HTTP requests to the server.

0
source share

You have another solution that is a third-party solution, perhaps a second solution, but it can also be the first. The third party is called GSON, and it is an open source project in Google code. You can find it here .

I used it, and it is very good and very simple.

0
source share

All Articles