【问题标题】:Convert LinkedHasmap to POJO?将 LinkedHashmap 转换为 POJO?
【发布时间】:2015-11-23 09:16:51
【问题描述】:

我有一个名为 Container 的 POJO,我想将它发送到 RESTful Web 服务。

客户端

    try {

       JAXBContext ctx = JAXBContextFactory.createContext(new Class[] {Container.class}, null);             
       jsonContent = objectToJSON(ctx, cont);
       HttpEntity entity = new ByteArrayEntity(jsonContent.getBytes("UTF-8"));
       ((HttpPost) httpUriRequest).setEntity(entity);

    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

     public String objectToJSON(JAXBContext ctx, Object object)
            {
                try
                {
                    Marshaller marshaller = ctx.createMarshaller();

                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
                    marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);

                    StringWriter sw = new StringWriter();
                    marshaller.marshal(object, sw);

                    return sw.toString();
                }
                catch (JAXBException e){

                    System.out.println("--------JAXB EXCEPTION------------\n" );
                    e.printStackTrace();
                    LOGGER.error("JAXB marshalling error!", e);
                }
                return "HELLO";
            }

我正在使用 REST-easy。

服务器端

@POST

    @Consumes({"application/json"})
    @Produces({"application/json"})
    public String handlePostRequest(Object resource) {

       System.out.println("\nINSIDE HANDLE POST: CONTENT CLASS" + resource.getClass());
       System.out.println("\nINSIDE HANDLE POST: " + resource);
        return "SIDD";
    }

在这里,我收到以Object 发送的内容类型: public String handlePostRequest(Object resource) {

所以 SOP 将类打印为 LinkedHashMap,并且我的 POJO (Container) 的属性在 HashMap 中设置为键值对。

这里我想使用Object 类型,因为从客户端发送的 POJO 可能因请求而异。所以我想写一个泛型函数。

现在,有没有一种标准方法可以将LinkedHashMap(作为对象)转换为我在 java 中的 POJO?

【问题讨论】:

    标签: java jaxb resteasy pojo


    【解决方案1】:

    您可以像这样将地图对象包装在 POJO 中:

    @Getter
    @Setter
    public class MyResource {
      @JsonProperty("map")
      private Map<Integer,Object> map;  // or LinkedHashMap if you want
    
      // You can have other fields here
    }
    

    您的方法签名如下所示:

    @POST
    @Consumes("application/json")
    @Produces("application/json")
    public String handlePostRequest(MyResource resource) {
       // Access your map
       resource.getMap();
    }
    

    JacksonJaxbJsonProvider 为您进行编组和解组。 RESTful 服务的输入如下所示:

    {
    "map": {
        "1" : "Hello",
        "2" : "World",
        ...
      }
    }
    

    【讨论】:

    • 在这种情况下,客户端代码是什么?例如目前我做的:JAXBContext ctx = JAXBContextFactory.createContext(new Class[] {Container.class}, null); jsonContent = objectToJSON(ctx, cont);。这提供了一个Container类的json字符串
    • 您可以使用com.google.gson 中的GsonBuilder 将Object 转换为JSON;类似new GsonBuilder().create().toJson(yourObject)。然后将此 JSON 提供给请求正文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2018-12-13
    • 2019-07-06
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多