0

I have a problem reading "traits" from an XML file and filling a table in a database. I successfully read teacher and student list but I get error trying to read traits. For parsing I am using jackson-dataformat-xml. Any help would be really appreciated.

The error that I am getting:

JSON parse error: Cannot deserialize value of type `java.util.ArrayList<com.Homework.Structure.Trait>` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<com.Better.Homework.Structure.Trait>` from Object value (token `JsonToken.START_OBJECT`)\n 

This is my XML file:

<teacher id="100" class="English">
    <students>
        <student>
            <id>1</id>
            <first_name>Alice</first_name>
            <last_name>Wild</last_name>
            <traits>
                <trait>nice</trait>
                <trait>good_grades</trait>
            </traits>
        </student>
        <student>
            <id>2</id>
            <first_name>John</first_name>
            <last_name>Doe</last_name>
            <traits>
                <trait>kind</trait>
                <trait>likes_to_help</trait>
            </traits>
        </student>
    </students>
</teacher>
@Data
@Entity
@Table
public class Teacher{

    @SequenceGenerator(
            name = "teacher_sequence",
            sequenceName = "teacher_sequence",
            allocationSize = 1
    )

    @JacksonXmlProperty(isAttribute = true, localName="id") private Integer id;
    @JacksonXmlProperty(isAttribute = true, localName = "class") private String class;

    @OneToMany (cascade = CascadeType.ALL)
    private List<Student> students;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
@Data
@Entity
@Table
public class Student{
    @SequenceGenerator(
            name = "student_sequence",
            sequenceName = "student_sequence",
            allocationSize = 1
    )

    @Id
    private Integer id;
    private String first_name;
    private String last_name;

    @OneToMany (cascade = CascadeType.ALL)
    private List <Trait> traits;

//    @ElementCollection
//    private List <String> traits;
}
@Table
@Data
@Entity
public class Trait {
    @SequenceGenerator(
            name = "trait_sequence",
            sequenceName = "trait_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "trait_sequence"
    )

    @Id
    private String traits;


//    @JsonCreator
//    public Trait(@JsonProperty("traits") String trait) {
//        this.trait = trait;
//    }

    public Trait() {

    }
}
4
  • Where is the patient class ? Commented Nov 10, 2021 at 12:11
  • I've just edited, didn't want to post the exact original code because of the privacy, so I've changed the class names and xml attributes. Thanks for the comment. Commented Nov 10, 2021 at 12:15
  • You have a JSON Parse Error for an XML document? Sounds weird... Commented Nov 10, 2021 at 20:11
  • I would guess that Jackson is complaining because <traits> is a sequence containing <trait> and your Trait class has a property called traits and thus it can not map <trait> to Trait. As an aside if it were my code I would create immutable classes to represent the inbound XML received and separate classes for the database entities, thus separating the two concerns. Commented Nov 11, 2021 at 10:03

1 Answer 1

0

You need to create a Traits class, which is like this ...

class Traits {
  List<Trait> traits
}

The student then has an instance of the Traits class composed into it. This will allow you to deserialize, I think.

Does this resolve your problem ? Let me know in the comments.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunetly that does not solve my problem because when I try to add list of Traits I get warning: 'Basic' attribute type should not be a container.
Do you get a warning or compilation/runtime error ?
Also you can do like this stackoverflow.com/a/12183957/2599899

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.