当使用xml进行网络传输时,一般会有请求头和请求体,不同的请求会有不同的请求体,当转换为实体类时,若包含父子关系,我们一般都会使用一下方式转换。
@JacksonXmlRootElement
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class A<T extends B> {
@JacksonXmlProperty(localName = "B")
private T b;
}
@JacksonXmlRootElement
@JsonInclude(JsonInclude.Include.NON_NULL)
public class B {
}
@Data
public class C extends B {
@JacksonXmlProperty(localName = "Test1")
private String test1;
@JacksonXmlProperty(localName = "Test2")
private String test2;
}
@Data
public class D extends B {
@JacksonXmlProperty(localName = "Test3")
private String test3;
@JacksonXmlProperty(localName = "Test4")
private String test4;
}
public static void main(String[] args) throws JsonProcessingException {
XmlMapper mapper = new XmlMapper();
A a = new A();
C c = new C();
c.setTest1("daffad");
c.setTest2("eqrewrqrew");
a.setB(c);
String xml = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(a);
System.out.println(xml);
JavaType javaType = mapper.getTypeFactory().constructParametricType(A.class,C.class);
A a1 = mapper.readValue(xml, javaType);
C b1 = (C) a1.getB();
System.out.println(b1.getTest1());
}
以上方式必须指定需要转换的子类,但有时我们需要根据某些字段动态转换成不同的子类,比如对请求进行编解码后是xml字符串,然后需要将xml字符串转换成实体类,这时就需要我们自定义解析器来实现,并使用@JsonDeserialize注解,如下
@JacksonXmlRootElement
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@JsonDeserialize(using = ADeserializer.class)
public class A {
@JacksonXmlProperty(localName = "Header")
private Header header;
@JacksonXmlProperty(localName = "B")
private B b;
}
@JacksonXmlRootElement
@JsonInclude(JsonInclude.Include.NON_NULL)
public class B {
}
@Data
public class C extends B {
@JacksonXmlProperty(localName = "Test1")
private String test1;
@JacksonXmlProperty(localName = "Test2")
private String test2;
}
@Data
public class D extends B {
@JacksonXmlProperty(localName = "Test3")
private String test3;
@JacksonXmlProperty(localName = "Test4")
private String test4;
}
@JacksonXmlRootElement
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class Header {
@JacksonXmlProperty(localName = "Type")
private String type;
}
public class ADeserializer extends JsonDeserializer<A> {
@Override
public A deserialize(JsonParser parser, DeserializationContext ctx) throws IOException, JsonProcessingException {
A a = new A();
TreeNode treeNode = parser.getCodec().readTree(parser);
TreeNode headerNode = treeNode.get("Header");
Header header = parser.getCodec().treeToValue(headerNode, Header.class);
TreeNode b = treeNode.get("B");
a.setHeader(header);
if ("1".equals(header.getType())) {
C c = parser.getCodec().treeToValue(b, C.class);
a.setB(c);
}
if ("2".equals(header.getType())) {
D d = parser.getCodec().treeToValue(b, D.class);
a.setB(d);
}
return a;
}
}
public static void main(String[] args) throws JsonProcessingException {
XmlMapper mapper = new XmlMapper();
A a = new A();
C c = new C();
Header header = new Header();
header.setType("1");
a.setHeader(header);
c.setTest1("daffad");
c.setTest2("eqrewrqrew");
a.setB(c);
String xml = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(a);
System.out.println(xml);
A a1 = mapper.readValue(xml, A.class);
C b1 = (C) a1.getB();
System.out.println(b1.getTest1());
}
以上就是通过自定义Jackson解析器实现xml转对象的例子,json转对象和以上是一样的,只不过使用的是json注解