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

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

问题描述

我知道 GWT 有很好的 RPC 支持.但出于各种目的,我需要自己构建:

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

1.) 如何转换 Bean 对象(在客户端),例如;

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

class MyPerson {

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

用 GWT 转换成 JSON 字符串?(理想情况下只使用正式来自 GWT/Google 的库).

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

2.) 其次,我如何将这个生成的 JSON 字符串从客户端发送到任何使用 GWT 客户端 Logik 的服务器.(理想情况下只使用正式来自 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).

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

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

非常感谢!!!延斯

推荐答案

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

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

拥有 AutoBeanFactory 后,您可以像这样使用它:

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

从 SimpleInterface 类型的对象生成 JSON

producing JSON from an object of type SimpleInterface

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

useRequestBuilderToSendRequestWhereverYouWant(requestData);

从 SimpleInterface 类型的对象解析 JSON

parsing JSON from an object of type SimpleInterface

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

您可以使用 RequestBuilder 发送这些请求,而无需 GWT-RPC 或 RF 东西.

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

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

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