问题描述
我正在使用 jackson-dataformat-xml 来封送具有列表属性的对象。列表的元素类型是一个具有多种实现的接口。我试图在类级别指定每个实现的 XML 类型,以便在封送列表元素时,根据每个元素的动态类型动态解析标签名称。
这是我迄今为止尝试过的 MWE:
public class Main {
public static void main(String[] args) throws IOException {
A a = new A(
List.of(
new C("property of first B (C)"),new C("property of second B (C)"),new D("property of third B (D)")
)
);
XmlMapper mapper = new XmlMapper();
mapper.registerModule(new JaxbAnnotationModule());
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.writeValue(System.out,a);
}
}
@JacksonXmlRootElement(localName = "foo")
class A {
// When marshalling the elements of this list,I want the tag names to be
// "dynamically" resolved to the names assigned to the class implementations of B
@JacksonXmlProperty
@JacksonXmlElementWrapper(useWrapping = false)
public final List<B> bList;
public A(List<B> bList) {
this.bList = bList;
}
}
interface B {}
@XmlType(name = "bar")
class C implements B {
@JacksonXmlProperty(localName = "cProperty",isAttribute = true)
public final String property;
public C(String property) {
this.property = property;
}
}
@XmlType(name = "baz")
class D implements B {
@JacksonXmlProperty(localName = "dProperty",isAttribute = true)
public final String property;
public D(String property) {
this.property = property;
}
}
打印:
<foo>
<bList cProperty="property of first B (C)"/>
<bList cProperty="property of second B (C)"/>
<bList dProperty="property of third B (D)"/>
</foo>
我正在努力实现的目标:
<foo>
<bar cProperty="property of first B (C)"/>
<bar cProperty="property of second B (C)"/>
<baz dProperty="property of third B (D)"/>
</foo>
对我来说重要的是能够在类级别指定标签名称,而不管实现这一点的确切方法。这可能吗?我该怎么做?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)