我似乎无法找到一种方法来制作 Pojo 使用会生成如下 xml 的 jackson-xml 注释:
<Root>
<Element1 ns="xxx">
<Element2 ns="yyy">A String</Element2>
</Element1>
</Root>
我似乎最接近的是:
根 POJO:
public class Root {
@JacksonXmlProperty(localName = "Element1")
private Element1 element1;
public String getElement1() {
return element1;
}
public void setElement1(String element1) {
this.element1 = element1;
}
}
Element1 POJO:
public class Element1 {
@JacksonXmlProperty(isAttribute = true)
private String ns = "xxx";
@JacksonXmlProperty(localName = "Element2")
private Element2 element2;
public String getElement2() {
return element2;
}
public void setElement2(String element2) {
this.element2 = element2;
}
}
Element2 POJO:
public class Element2 {
@JacksonXmlProperty(isAttribute = true)
private String ns = "yyy";
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
但这会返回以下内容:
<Root>
<Element1 ns="xxx">
<Element2 ns="yyy"><value>A String</value></Element2>
</Element1>
</Root>
“A String”周围的元素标签我不想显示。
最佳答案
您应该使用 JacksonXmlText value
字段的注释。
public class Element2
{
@JacksonXmlProperty(isAttribute = true)
private String ns = "yyy";
@JacksonXmlText
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
然后 XML 看起来像
<Root>
<Element1 ns="xxx">
<Element2 ns="yyy">A String</Element2>
</Element1>
</Root>
关于java - jackson XML 注释 : String element with attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19847094/