jacksonxml使用方法?
public class Demo1 {
public static void main(String[] args) {
XmlMapper xmlMapper = new XmlMapper();
//字段為null,自動忽略,不再序列化
xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Department department = new Department();
Employees employees1 = new Employees("秋一葉",18,"男");
Employees employees2 = new Employees("小王",20,"男");
department.setDepName("產品研發(fā)部門");
department.setEmployees(Lists.newArrayList(employees1, employees2));
try {
//對象轉xml
String xml = xmlMapper.writeValueAsString(department);
//xml轉對象
Department dep = xmlMapper.readValue(xml, Department.class);
System.out.println("返回的xml:" + xml);
System.out.println("返回的實體:" + dep);
} catch (JsonProcessingException e) {
e.printStackTrace();