RETEasy @WrappedMap

发布于 2024-10-06 17:35:02 字数 901 浏览 0 评论 0原文

我正在使用 RESTEasy 编写 RESTful Web 服务,并尝试编写包含 HashMap 的响应。 Web 服务生成 JSON 或 XML。 JSON 映射是正确的,但 XML 映射没有内容。 RESTEasy @WrappedMap 注释应该将 Map 编组为 XML。

@XmlRootElement(name="Response")
public 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 输出:

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

XML 输出:

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

如果省略 @WrappedMap 注释,我会得到相同的输出。

@WrappedMap 不适用于属性吗?

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")
public 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2024-10-13 17:35:02

我知道你发布这个问题已经有一段时间了,但我仍然想分享我最近学到的东西,以防其他人偶然发现这个问题。

@WrappedMap 根据 本文档仅帮助更改输出 XML 中的元素名称。至于为什么你不能让JAXB从HashMap输出XML,我认为此页有说明。

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文