问题描述
我正在尝试反序列化来自RestTemplate
的XML
响应,并且该响应包含我需要在映射对象中设置的属性.响应类似于:
I'm trying to deserialise an XML
response from my RestTemplate
and the response contains attributes that I need to set in my mapped object. The response is similar to:
<schoolResponse>
<class id="1" num_of_students="22" name="Ms Barry" >
<student key="202" value="Jim" />
<student key="203" value="Tom" />
<student key="204" value="Dave" />
<schoolYear>1980</schoolYear>
</class>
<class id="2" num_of_students="20" name="Mr Smith">
<student key="302" value="Jim" />
<student key="303" value="Tom" />
<student key="304" value="Dave" />
<schoolYear>1980</schoolYear>
</class>
<class>
...
</class>
</schoolResponse>
我的POJO
是:
@Data
@NoArgsConstructor
public class SchoolResponse {
@JacksonXmlElementWrapper(localName = "class")
private List<ClassRoomResponse> classes;
}
@Data
@NoArgsConstructor
@XmlRootElement(name="class")
public class ClassRoomResponse {
@XmlAttribute(name = "id")
private String id;
@XmlAttribute(name = "num_of_students")
private String numOfStudents;
@XmlAttribute(name = "name")
private String teacherName;
@XmlElement(name = "schoolYear")
private String schoolYear;
@JacksonXmlElementWrapper(localName = "key")
String key;
@JacksonXmlElementWrapper(localName = "value")
String value;
public ClassRoomResponse(String stringSetter) {}
}
此映射未正确设置值,因此,我想知道这些属性遇到哪些问题.
This mapping hasn't been setting the values properly, so, I'm wondering what issues I'm running into with these attributes.
推荐答案
对于Student
节点,您需要一个额外的POJO
.另外,您的所有收藏都已展开.如果混合使用JAXB
和Jackson
批注,则需要注册com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule
模块.请参见下面的示例,其中包含固定的POJO
类:
You need an extra POJO
for Student
node. Also, all your collections are unwrapped. In case when you mix JAXB
and Jackson
annotations you need to register com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule
module. See below example with fixed POJO
classes:
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class XmlMapperApp {
public static void main(String... args) throws IOException {
File xmlFile = new File("./resource/test.xml").getAbsoluteFile();
XmlMapper xmlMapper = XmlMapper.xmlBuilder()
.addModule(new JaxbAnnotationModule())
.build();
System.out.println(xmlMapper.readValue(xmlFile, SchoolResponse.class));
}
}
@Data
@NoArgsConstructor
@ToString
class SchoolResponse {
@XmlElement(name = "class")
@JacksonXmlElementWrapper(useWrapping = false)
private List<ClassRoomResponse> classes;
}
@Data
@NoArgsConstructor
class ClassRoomResponse {
@XmlAttribute(name = "id")
private String id;
@XmlAttribute(name = "num_of_students")
private String numOfStudents;
@XmlAttribute(name = "name")
private String teacherName;
@XmlElement(name = "schoolYear")
private String schoolYear;
@XmlElement(name = "student")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Student> students;
}
@Data
@NoArgsConstructor
@ToString
class Student {
@XmlAttribute(name = "key")
String key;
@XmlAttribute(name = "value")
String value;
}
上面的代码显示:
SchoolResponse(classes=[ClassRoomResponse(id=1, numOfStudents=22, teacherName=Ms Barry, schoolYear=1980, students=[Student(key=202, value=Jim), Student(key=203, value=Tom), Student(key=204, value=Dave)]), ClassRoomResponse(id=2, numOfStudents=20, teacherName=Mr Smith, schoolYear=1981, students=[Student(key=302, value=Jim), Student(key=303, value=Tom), Student(key=304, value=Dave)])])
这篇关于使用属性而不是包装的集合反序列化XML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!