Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring Nested properties in Jackson OnDemand

I am working on a spring boot application with Hibernate as ORM and Jackson as JSON serialiser .

I have three model objects and CRUD operations for all three models.

Class Student{
     private Teacher teacher;  // Teacher of the student — to be fetched eagerly
    +Getter/Setter
}

class Teacher {
      private List<Subject> subject;  // List of subjects associated to that user— to be fetched eagerly
      +Getter/Setter 
}

class Subject {
     private long subjectId 
    //Other subject properties
    + Getter/Setter
}

Whenever I trigger a get request for student info I get the teacher info which is correct where as I also receive Subject info as well which is unnecessary for me. In the same time when I request for Teacher info, I need Subject info should be associated to that for sure. If I use @JsonBackReference for subject I am losing it all the time. I am not sure how to achieve this.

Thanks in advance for your help!!

like image 866
Jahir Avatar asked Mar 09 '23 06:03

Jahir


1 Answers

You can also annotate like this

Class Student{
    @JsonIgnoreProperties("subject")
    private Teacher teacher;  // Teacher of the student — to be fetched eagerly
}
like image 196
danidacila Avatar answered Mar 21 '23 08:03

danidacila