Deserialise XML response with attributes and not wrapped collections

坚强是说给别人听的谎言 提交于 2020-08-10 05:40:05

问题


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>

My POJO are:

@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.


回答1:


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;
}

Above code prints:

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)])])


来源:https://stackoverflow.com/questions/59256513/deserialise-xml-response-with-attributes-and-not-wrapped-collections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!