开发者

RESTEasy @WrappedMap

开发者 https://www.devze.com 2023-01-29 08:33 出处:网络
I am using RESTEasy to write a RESTful web service and trying to write a response that contains a HashMap.The web service produces either JSON or XML.The JSON map is correct, but the XML map has no co

I am using RESTEasy to write a RESTful web service and trying to write a response that contains a HashMap. The web service produces either JSON or XML. The JSON map is correct, but the XML map has no content. The RESTEasy @WrappedMap annotation is supposed to marshal Maps to XML.

@XmlRootElement(name="Response")
publi开发者_开发百科c class RootResponse {
  private HashMap<String, String> test;
  public RootResponse() {
    test = new HashMap<String, String>();
    test.put("Fred", "Wilma");
    test.put("Barney", "Betty");
  }
  @XmlElement
  @WrappedMap(map="test", key="name", entry="spouse")
  public HashMap<String, String> getTest() {
    return this.test;
  }
}

JSON output:

{
  "test": {
    "Fred": "Wilma",
    "Barney": "Betty"
  }
}

XML output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response>
  <test/>
</Response>

I get the same output if I leave out the @WrappedMap annotation.

Does @WrappedMap not work for properties?


I know it has been a while since you posted this question, but I still want to share what I have recently learnt in case someone else may stumble upon this question.

@WrappedMap according to this documentation only helps changing the name of the elements in the output XML. As to why you cannot get JAXB to output XML from the HashMap, I think this page has the explanation.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消