[英]Java+Jackson+XML: serialize a java object properties as XML elements with same names
I have a Java object and I would like to serialize it into XML using Jackson library:我有一个 Java 对象,我想使用Jackson库将它序列化为XML :
public class Point {
private Integer x;
private Integer y;
//getters/setters
}
and I would like to serialize it into following format:我想将其序列化为以下格式:
<point>
<property name="x" value="1" />
<property name="y" value="1" />
</point>
instead of what I get using Jacskon:而不是我使用 Jacskon 得到的:
<point>
<x>1</x>
<y>1</y>
</point>
I do not want to change the Point
object properties or structure.我不想更改
Point
对象的属性或结构。 Is there a way to serialize the Point
object into required format using a Jackson annotations or custom serializer?有没有办法使用 Jackson 注释或自定义序列化程序将
Point
对象序列化为所需格式? If yes then how do I do that?如果是,那我该怎么做?
I am using Jackson library:我正在使用杰克逊库:
public class Serializer {
XmlMapper mapper = new XmlMapper();
public void serialize(File file, Object object) throws IOException {
mapper.writeValue(file, object);
}
}
You need to mark those properties as attributes like this:您需要将这些属性标记为如下属性:
public class Point {
@JacksonXmlProperty(isAttribute = true)
private Integer x;
@JacksonXmlProperty(isAttribute = true)
private Integer y;
//getters/setters
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.