如何更改中的默认 Hashmap 标签名称使用 JAXB 编组 XML 时是否会发生其他事情?
我正在尝试使用 JAXB 将编组 XML 中的标记值更改为 .调用我的应用程序的客户正在期待,以前我使用 XSLT 来执行此转换,但我正在切换到 JAXB 方法来创建 XML 响应。
现在,这是我正在创建的错误 XML:
<error>
<details>
<entry>
<key>code</key>
<value>1234</value>
</entry>
<entry>
<key>customKey</key>
<value>customerValue</value>
</entry>
</details>
</error>
我需要它看起来像这样:
<error>
<details>
<detail>
<key>code</key>
<value>1234</value>
</detail>
<detail>
<key>customKey</key>
<value>customerValue</value>
</detail>
</details>
</error>
我用来编组的 Java 对象如下:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Map;
@XmlRootElement(
name = "error"
)
@XmlAccessorType(XmlAccessType.FIELD)
public class Error {
private Map<String, String> details;
public Error() {
}
public Error(Map<String, String> details) {
this.details = details;
}
public Map<String, String> getDetails() {
return details;
}
public void setDetails(Map<String, String> details) {
this.details = details;
}
}
在将 Map 编组到 XML 时,是否有一种方法可以更改默认命名约定使用 JAXB?我是否可以使用嵌套对象而不是哈希映射以另一种方式来实现此目的?
我正在尝试创建一个
列表,但我的 JAXB 在编组列表时遇到了麻烦。谢谢
I am attempting to change the value of the tag in the marshalled XML using JAXB to . My clients that are calling my application are expecting , previously I was using XSLT to do this transformation but I am switching to a JAXB approach to create the XML response.
Right now this is the error XML that I am creating:
<error>
<details>
<entry>
<key>code</key>
<value>1234</value>
</entry>
<entry>
<key>customKey</key>
<value>customerValue</value>
</entry>
</details>
</error>
And I need it to look like this:
<error>
<details>
<detail>
<key>code</key>
<value>1234</value>
</detail>
<detail>
<key>customKey</key>
<value>customerValue</value>
</detail>
</details>
</error>
The Java object that I am using to marshall is as follows:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Map;
@XmlRootElement(
name = "error"
)
@XmlAccessorType(XmlAccessType.FIELD)
public class Error {
private Map<String, String> details;
public Error() {
}
public Error(Map<String, String> details) {
this.details = details;
}
public Map<String, String> getDetails() {
return details;
}
public void setDetails(Map<String, String> details) {
this.details = details;
}
}
Is there a way to change the default naming convention when marshalling a Map to XML using JAXB? Is there possibly I can do this another way with nested objects instead of a hash map?
I was messing around attempting to do a List of <details>
but my JAXB was having troubles marshalling a list.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有办法改变这个命名,因为元素名称
、
和
在 JAXB 实现中进行硬编码。在类 com.sun.xml.bind.v2.runtime.property.SingleMapNodeProperty 中
(它负责序列化
Map
条目)实际上有这样的代码:不知道你遇到了哪些困扰。
但是您只需将
Error
类从 更改为 即可实现您的目标(
当然还要相应地调整构造函数/getter/setter)。
您还需要一个
Detail
类来表示内容在
...
内。I think there is no way to change this naming, because the element names of
<entry>
,<key>
and<value>
are hard-coded in the JAXB implementation.In class
com.sun.xml.bind.v2.runtime.property.SingleMapNodeProperty
(which is responsible for serializing a
Map
entry) there is actually this code:I don't know which troubles you encountered.
But you can achieve your goal just by changing in your
Error
class fromto
(and of course also adapting the constructor/getter/setter accordingly).
You will also need a
Detail
class for representing the contentwithin
<detail>...</detail>
.