2

I have a XML file which i want to deserialize using Jackson.But i am getting the above 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

 @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; line: 2, column: 3]

I guess there is a problem with the annotations.I am a newbie to Jackson.So a help will be appreciated.

0

1 Answer 1

1

You need a no-arg constructor for Jackson to deserialize JSON to Java objects. In the deserialization process Jackson uses the attributes/methods either directly rather than constructors.

Try adding

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

or maybe

public Students() {
    this(new Student[]{});
}
Sign up to request clarification or add additional context in comments.

2 Comments

you meant to call this(new Student[]{});
@sharonbn Yes, apologies,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.