我有一个Java对象,我想使用Jackson库将其序列化为XML:
public class Point {
private Integer x;
private Integer y;
//getters/setters
}
并且我想将其序列化为以下格式:
<point>
<property name="x" value="1" />
<property name="y" value="1" />
</point>
而不是我使用Jacskon获得的内容:
<point>
<x>1</x>
<y>1</y>
</point>
我不想更改Point
对象的属性或结构。有办法吗使用Jackson注释或自定义序列化程序将Point
对象序列化为所需格式?如果是,那我该怎么办?
我正在使用Jackson库:
public class Serializer {
XmlMapper mapper = new XmlMapper();
public void serialize(File file, Object object) throws IOException {
mapper.writeValue(file, object);
}
}
您需要将这些属性标记为如下属性:
public class Point {
@JacksonXmlProperty(isAttribute = true)
private Integer x;
@JacksonXmlProperty(isAttribute = true)
private Integer y;
//getters/setters
}