GWT (Client) = How to convert Object to JSON and send to Server?

后端 未结 4 1379
感情败类
感情败类 2020-12-30 11:08

I know that GWT has a good RPC support. But for various purposes I need to build this on my own:

1.) How can I convert a Bean Object (on the Client Side) like;

相关标签:
4条回答
  • 2020-12-30 11:35

    I recommend you use RestyGWT it makes JSON rest services work just like GWT RPC services.

    0 讨论(0)
  • 2020-12-30 11:43

    Take a look at GWT's AutoBean framework, which can be used to create and receive JSON payloads. The RequestBuilder type can be used to send HTTP requests to the server.

    0 讨论(0)
  • 2020-12-30 11:47

    You have also another solution which is 3rd party solution, maybe a second place solution but it can be also the first place. The 3rd party called GSON and it's a project open source on google code. You can find it here.

    I used it and it's very good and very simple.

    0 讨论(0)
  • 2020-12-30 11:54

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

    Once you have your AutoBeanFactory, you can use it like this:

    producing 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 the RF stuff.

    0 讨论(0)
提交回复
热议问题