Sid*_*kha 7 xml json jaxb jackson jackson-dataformat-xml
我有一个要支持JSON和XML数据格式序列化/反序列化的XSD。
我使用xjc实用程序生成了Model类。
因此,到目前为止,我已经使用Jackson JSON库处理了JSON数据。
我无法修改Java类,因此我配置ObjectMapper了Mix-In annotations和其他功能PropertyNamingStrategy(如更改字段名),SerializationFeature.WRAP_ROOT_VALUE以通过代码在我的序列化过程中提供配置。
现在,我想对XML序列化过程做同样的事情。
我已经在线阅读了各种选择:
哪一种最适合我的情况(不能编辑带注释的POJO,仅允许代码配置)?
我的票是 #2 Jackson library + Jackson-dataformat-xml.jar 看看 JSON 和 XML 的代码,这里和那里的位变化都是一样的。
****MainClass****
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class MainClass {
public static void main(String[] args) throws JsonProcessingException {
// Serialization: java obj to json--> writeValueAsString
// DeSerialization: json to java obj--> ReadValue
XmlMapper mapper1 = new XmlMapper();
ObjectMapper mapper2 = new ObjectMapper();
mapper1.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper2.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper1.enable(SerializationFeature.INDENT_OUTPUT);
mapper2.enable(SerializationFeature.INDENT_OUTPUT);
MyPojo mypojo = new MyPojo();
mypojo.setName("Dhani");
mypojo.setId("18082013");
mypojo.setAge(5);
String jsonStringXML = mapper1.writeValueAsString(mypojo);
String jsonStringJSON = mapper2.writeValueAsString(mypojo);
// takes java class with def or customized constructors and creates JSON
System.out.println("XML is " + "\n" + jsonStringXML + "\n");
System.out.println("Json is " + "\n" + jsonStringJSON);
} }
*****MyPojo.java*****
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JsonPropertyOrder({ "name", "age", "id", "note" })
@JacksonXmlRootElement(namespace = "urn:stackify:jacksonxml", localName = "myPOJO")
public class MyPojo {
@JsonProperty("_id")
private String id;
private String name;
private int age;
@JsonIgnore
private String note;
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
***RESULT****
XML is
<myPOJO xmlns="urn:stackify:jacksonxml">
<name xmlns="">Dhani</name>
<age xmlns="">5</age>
<_id xmlns="">18082013</_id>
</myPOJO>
Json is
{
"name" : "Dhani",
"age" : 5,
"_id" : "18082013"
}
Run Code Online (Sandbox Code Playgroud)
我投票支持答案 #2:使用 Jackson-dataformat-xml。我们遇到了类似的情况,发现通过 Jackson 序列化到 XML 比预期的要容易。这是因为XmlMapperextendsObjectMapper因此我们所有的配置更改(混入、使用JavaTimeModule等)都可以直接应用于XmlMapper并且它们只是起作用。
您已经将 Jackson 配置为根据需要输出数据,因此最困难的部分已经完成。我建议利用您已经投入的精力并使用 Jackson-dataformat-xml。