GWT(客户端)=如何将对象转换为JSON并发送到服务器? [英] GWT (Client) = How to convert Object to JSON and send to Server?

查看:525
本文介绍了GWT(客户端)=如何将对象转换为JSON并发送到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道GWT有很好的RPC支持。但为了各种目的,我需要自己构建它:



1。)如何将Bean对象(在客户端)转换为像

  class MyPerson {

字符串名称;
String getName();
void setName(String name);
// ..
}

与GWT合并为一个JSON字符串? (理想情况下,只使用来自GWT / Google的正式库)。其次,我怎样才能将这个生成的JSON字符串从客户端发送到任何服务器使用任何GWT客户端Logik。 (理想情况下,只使用来自GWT / Google的正式库)。

我搜索了很多,但这些示例从未展示如何发送数据,但仅接收JSON数据。



非常感谢!
Jens

解决方案

GWT将为您创建一个称为AutoBeanFactory的漂亮类,不需要任何第三方库。请参阅 http://google-web-toolkit.googlecode.com/svn-history/r9219/javadoc/2.1/com/google/gwt/editor/client/AutoBeanFactory.html



获得AutoBeanFactory后,您可以像这样使用它:

从SimpleInterface类型的对象生成JSON

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

useRequestBuilderToSendRequestWhereverYouWant(requestData);

从SimpleInterface类型的对象解析JSON

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

您可以使用 RequestBuilder 发送这些请求没有GWT-RPC或RF的东西。


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;

class MyPerson {

String name;
String getName();
void setName(String name);
//..    
}

with GWT into a JSON String? (Ideally only using libraries that come officially from GWT/Google).

2.) Secondly, how can I send this generated JSON String from the Client side to any Server also using any GWT Client Logik. (Ideally only using libraries that come officially from GWT/Google).

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

Thank you very much!!! Jens

解决方案

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.

这篇关于GWT(客户端)=如何将对象转换为JSON并发送到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆