我将Java POJO序列化为xml并想要更改xml标记名称。我无法正常工作,但@JacksonXmlElementWrapper和@JacksonXmlProperty注释无法获取。
Maven pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>TestJackson</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
</properties>
<repositories>
<repository>
<id>central</id>
<name>Maven repository</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>jackson-xml-databind</artifactId>
<version>0.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>5.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Java类:
package com.company;
import com.fasterxml.jackson.dataformat.xml.annotation.*;
import java.util.List;
public class OrderItem {
private String myId = null;
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "service")
private List services = null; //
public void setId(String myId) {
this.myId = myId;
}
public void setServices(List myServices) {
this.services = myServices;
}
public String getId() {
return myId;
}
public List getServices() {
return services;
}
}
package com.company;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.util.ArrayList;
import java.util.List;
public class Order {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "items")
private List items = new ArrayList();
public void setItems(List items) {
this.items = items;
}
public List getItems() {
return items;
}
}
package com.company;
import com.fasterxml.jackson.dataformat.xml.annotation.*;
import java.util.List;
public class OrderItem {
private String myId = null;
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "service")
private List services = null; //
public void setId(String myId) {
this.myId = myId;
}
public void setServices(List myServices) {
this.services = myServices;
}
public String getId() {
return myId;
}
public List getServices() {
return services;
}
}
生成的xml不是我所期望的。我做错了什么?
<Order>
<items>
<items>
<services>
<services>service00</services>
<services>service01</services>
<services>service02</services>
<services>service03</services>
<services>service04</services>
</services>
<id>id0</id>
</items>
<items>
<services>
<services>service10</services>
<services>service11</services>
<services>service12</services>
<services>service13</services>
<services>service14</services>
</services>
<id>id1</id>
</items>
<items>
<services>
<services>service20</services>
<services>service21</services>
<services>service22</services>
<services>service23</services>
<services>service24</services>
</services>
<id>id2</id>
</items>
<items>
<services>
<services>service30</services>
<services>service31</services>
<services>service32</services>
<services>service33</services>
<services>service34</services>
</services>
<id>id3</id>
</items>
<items>
<services>
<services>service40</services>
<services>service41</services>
<services>service42</services>
<services>service43</services>
<services>service44</services>
</services>
<id>id4</id>
</items>
</items>
</Order>
答案 0 :(得分:2)
通过更改XMLMapper的包来解决。正在使用:
com.fasterxml.jackson.xml.XmlMapper
现在使用:
com.fasterxml.jackson.dataformat.xml.XmlMapper
我创建了映射器:
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper mapper = new XmlMapper(module);
字段名称可以更改为:
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
@JacksonXmlProperty(localName = "personaldetails")