简体   繁体   English

JSON到POJO,反之亦然,适用于GWT

[英]JSON to POJO and vise-versa for GWT

My applications use common Models integrated as dependencies for multiple application. 我的应用程序使用集成为多个应用程序的依赖关系的通用模型 One of the app the has a dependency on the Models have a Restlet connection through Restlet: 其中一个依赖模型的应用程序通过Restlet具有Restlet连接:

ClientResource res = new ClientResource("/path");
            res.setOnResponse(new Uniform() {
                @Override
                public void handle(Request request, Response response) {
                    try {
                        if(!response.getStatus().isError()){
                           String body = response.getEntity().getText();
                          // do stuff with Response JSON
                        } else {

                        }
                    } catch (IOException e) {
                       // handle error
                    }
                }
            });
res.post(new JsonRepresentation(MediaType.APPLICATION_JSON, jsonObject));

I have two questions for this, 我对此有两个问题,

  1. Is there a straightforward way for marshalling a POJO into a com.google.gwt.json.client.JSONObject in GWT for use with the res.post above 有没有一种简单的方法可以将POJO编组到GWT中的com.google.gwt.json.client.JSONObject中,以与上面的res.post一起使用
  2. Is there a straightforward way for marshalling a JSON String into a POJO with GWT? 是否有使用GWT将JSON字符串编组为POJO的简单方法?

That is both applicable in the ClientResource above as well as not resort into using GWT Overlay types as we already a Model shared in both client and server-side. 这既适用于上面的ClientResource ,也不能使用GWT Overlay类型,因为我们已经在客户端和服务器端共享了一个Model。 Something that is straightforward. 这很简单。

  1. AFAIK there is no automated and easy way to convert a POJO into JSONObject s. AFAIK没有将POJO转换为JSONObject的自动简便方法。 Most likely you would have to do it by hand. 您最有可能必须手动执行此操作。 The JsonRepresentation constructor however accepts a JSON String, so you could use on of the alternatives (see point 2) 但是, JsonRepresentation构造函数接受JSON字符串,因此您可以使用其中的一种(参见第2点)
  2. There are a couple of alternative easier ways to convert POJOs to JSON: 有几种将POJO转换为JSON的简便方法:

I guess if you have common POJO's the easiest solution would be to use AutoBeans . 我想如果您有通用的POJO,最简单的解决方案就是使用AutoBeans You would define an interface with all the setters and getters for each POJO and have the POJO's implement those interfaces. 您将为每个POJO定义一个包含所有setter和getter的接口,并让POJO实现这些接口。

The bean / JSON conversion isn't supported by Restlet out of the box. 开箱即用的Restlet不支持bean / JSON转换。 If you want to use beans within Restlet, you should use the native GWT protocol. 如果要在Restlet中使用bean,则应使用本机GWT协议。 It's the guidance that chose Restlet for its GWT implementation. 正是由于该指南,Grest实施才选择Restlet。 You could have the content types enabled within Restlet and select the one you want with the content negotiation (GWT RPC for GWT clients and JSON for others). 您可以在Restlet中启用内容类型,然后通过内容协商选择所需的内容类型(GWT客户端使用GWT RPC,其他使用JSON)。

That said, I understand your approach. 也就是说,我了解您的方法。 Restlet leverages the generator feature to directly work on beans in the case of the GWT RPC (see this class https://github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org/restlet/rebind/ClientProxyGenerator.java.gwt ). 在GWT RPC的情况下,Restlet利用生成器功能直接在bean上工作(请参阅此类https://github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org /restlet/rebind/ClientProxyGenerator.java.gwt )。 The generator only support the media type for GWT RPC but could be extended to handle other media types like JSON. 该生成器仅支持GWT RPC的媒体类型,但可以扩展为处理其他媒体类型,例如JSON。 Notice that such approach is tricker that the one provided by Ümit in its answer. 请注意,这样的做法是,一个提供tricker 乌米特在其答案。

Hope it give you some hints to fix your problem, Thierry Thierry希望它能给您一些解决问题的提示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM