java GWT(客户端)=如何将对象转换为 JSON 并发送到服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5658936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:08:27  来源:igfitidea点击:

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

javagwt

提问by jens

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

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

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

1.) 我怎样才能像这样转换一个 Bean 对象(在客户端);

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).

将 GWT 转换为 JSON 字符串?(理想情况下只使用正式来自 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).

2.) 其次,如何将这个生成的 JSON 字符串从客户端发送到任何服务器,也使用任何 GWT 客户端 Logik。(理想情况下只使用正式来自 GWT/Google 的库)。

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

我搜索了很多,但示例从未显示如何发送数据而只显示接收 JSON 数据。

Thank you very much!!! Jens

非常感谢你!!!延斯

回答by Riley Lark

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

GWT 将为您创建一个名为 AutoBeanFactory 的漂亮类,不需要第三方库。请参阅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:

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

producing JSON from an object of type SimpleInterface

从 SimpleInterface 类型的对象生成 JSON

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 类型的对象解析 JSON

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

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

您可以RequestBuilder在没有 GWT-RPC 或 RF 的情况下发送这些请求。

回答by Hiram Chirino

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

我建议您使用RestyGWT,它使 JSON 休息服务像 GWT RPC 服务一样工作。

回答by Dor

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.

您还有另一个解决方案,它是第 3 方解决方案,可能是第二位解决方案,但它也可以是第一名。第 3 方称为 GSON,它是 Google 代码上的一个开源项目。你可以在这里找到它。

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

我用过它,它非常好,非常简单。

回答by BobV

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

查看 GWT 的AutoBean框架,该框架可用于创建和接收 JSON 有效负载。该RequestBuilder类型可用于向服务器发送 HTTP 请求。