RETEasy @WrappedMap
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道你发布这个问题已经有一段时间了,但我仍然想分享我最近学到的东西,以防其他人偶然发现这个问题。
@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 theHashMap
, I think this page has the explanation.Hope this helps.