public static void main(String[] args) throws JsonProcessingException {
Person person = new Person();
person.setName("李四");
person.setAge(20);
ObjectMapper mapper = new XmlMapper()
// 序列化时加上文件头信息
.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
System.out.println(mapper.writeValueAsString(person));
}
输出结果:
<?xml version='1.0' encoding='UTF-8'?><Person><name>李四</name><age>20</age></Person>