简体   繁体   English

使用Jackson的XML到Java对象的反序列化

[英]XML to Java object deserialization using Jackson

I have a XML file which i want to deserialize using Jackson.But i am getting the above Exception. 我有一个要使用Jackson进行反序列化的XML文件,但是我收到了上面的Exception。

   <students>
     <student>
        <Name>Tapishnu2</Name>
        <Age>25</Age>
        <Department>Computer</Department>
     </student>
     <student>
        <Name>Tapishnu1</Name>
        <Age>25</Age>
        <Department>Computer</Department>
     </student>
      <student>
        <Name>Tapishnu2</Name>
        <Age>25</Age>
        <Department>Computer</Department>
     </student>
      <student>
        <Name>Tapishnu3</Name>
        <Age>25</Age>
        <Department>Computer</Department>
      </student>
   </students>

I have a POJO class like this 我有一个像这样的POJO课

 @JacksonXmlRootElement(localName = "students")
    public class Students {

    @JacksonXmlProperty(localName = "student")  
    private Student[] student;

    /*public Students(){

    }*/

    public Students( Student[] student) {
        super();
        this.student = student;
    }

    public Student[] getStudent() {
        return student;
    }

    public void setStudent(Student[] student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "students [student=" + Arrays.toString(student) + "]";
      }
    }

     public class Student {
    @JacksonXmlProperty(localName = "Name")
    String Name;

    @JacksonXmlProperty(localName = "Age")  
    String Age;

    @JacksonXmlProperty(localName = "Department")
    String Department;

    public String getName() {
        return Name;
    } 

    public void setName(String Name) {
        this.Name = Name;
    }

    public String getAge() {
        return Age;
    }

    public void setAge(String Age) {
        this.Age = Age;
    }
    public String getDepartment() {
        return Department;
    }
    public void setDepartment(String department) {
        Department = department;
    }

    @Override
    public String toString() {
        return "Student [Name=" + Name + ", Age=" + Age + ", Department="
                + Department + "]";
       }
      }

       Main class 

        file = new File("C://Avatar//Students.xml");
        System.out.println(file.canRead());  
        XmlMapper mapper = new XmlMapper();
        Students openCredentials = mapper.readValue(file, Students.class);

I am getting the following Error com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.Team.Students]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: C:\\Avatar\\Students.xml; 我收到以下错误com.fasterxml.jackson.databind.JsonMappingException:未找到类型[简单类型,类com.Team.Students]的合适构造函数:无法从JSON对象实例化(缺少默认构造函数或创建者,或者可能需要在[来源:C:\\ Avatar \\ Students.xml;中添加/启用类型信息? line: 2, column: 3] 行:2,列:3]

I guess there is a problem with the annotations.I am a newbie to Jackson.So a help will be appreciated. 我想批注有问题,我是Jackson的新手,请多多关照。

You need a no-arg constructor for Jackson to deserialize JSON to Java objects. 您需要Jackson的无参数构造函数才能将JSON反序列化为Java对象。 In the deserialization process Jackson uses the attributes/methods either directly rather than constructors. 在反序列化过程中,Jackson直接使用属性/方法,而不是使用构造函数。

Try adding 尝试添加

public Students() {
    super();
    this.student = new Student[]{};
}

or maybe 或者可能

public Students() {
    this(new Student[]{});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM