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

后端 未结 4 1402
感情败类
感情败类 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: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 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.

提交回复
热议问题