2

Let's say I an output class that will be converted to JSON:

StudentSummary.java

public class StudentSummary {

    private StudentList studentList;

    // getters setters
}

and

StudentList.java

public class StudentList {

    private int numberOfStudents;
    private int totalExpenditures;
    private List<Student> students;

    // getters setters
}

After making a service call, I get this JSON output:

{ 
 "studentSummary": {
     "studentList": {
         "numberOfStudents": 500,
         "totalExpenditures": 250000,
         "students": [ /* students listed */ ]
     }
}

I want to exclude from JSON the studentList in the StudentSummary class:

{ 
 "studentSummary": {
     "studentList": {
         "numberOfStudents": 500,
         "totalExpenditures": 250000
      }
  }
}

I've tried using (in the StudentSummary output class) @JsonIgnore and @JsonProperties , by specifying to only exclude "studentList.students", but that doesn't do anything.

EDIT: Further clarification, for why I couldn't do the changes inside the StudentList class, it's because it is used by another service and the service with StudentSummary class is of a different service, so I can only make the changes inside the latter class, without modifying the previous service.

laffuste
  • 16,287
  • 8
  • 84
  • 91
  • Have you tried to put the @JsonIgnore on the ´students´ field in StudentList class? – Kh.Taheri Jul 28 '17 at 11:55
  • @Kh.Taheri I have added an edit at the bottom, but basically, StudentList is used by a different service and the service I want to write right now, only needs the changes made of JSON output inside the StudentSummary class. – Hekky TwoFiveEightNine Jul 28 '17 at 12:00

6 Answers6

3

Summary

This will solve your problem:

public class StudentSummary {

    @JsonIgnoreProperties(value = { "students" })
    public StudentList studentList = new StudentList();

You do not need to edit the StudentList class.

All Test Class Definitions

Student

public class Student {

    String name = "Student" + Math.random()*100;

    public String getName() {
        return name;
    }

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

StudentList

public class StudentList {

    int numberOfStudents;
    int totalExpenditures;
    List<Student> students = new ArrayList<>();

    public StudentList(){
        for(int i =0; i<10; i++){
            students.add(new Student());
        }
    }

    public int getNumberOfStudents() {
        return numberOfStudents;
    }

    public void setNumberOfStudents(int numberOfStudents) {
        this.numberOfStudents = numberOfStudents;
    }

    public int getTotalExpenditures() {
        return totalExpenditures;
    }

    public void setTotalExpenditures(int totalExpenditures) {
        this.totalExpenditures = totalExpenditures;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    //getters setters
}

StudentSummary

public class StudentSummary {

    @JsonIgnoreProperties(value = { "students" })
    public StudentList studentList = new StudentList();

    public StudentSummary(){
        studentList = new StudentList();
    }

    public StudentList getStudentList() {
        return studentList;
    }

    public void setStudentList(StudentList studentList) {
        this.studentList = studentList;
    }

    //getters setters
}

Main Class

ObjectMapper mapper = new ObjectMapper();

StudentSummary summary = new StudentSummary();
String test = mapper.writeValueAsString(summary);
System.out.println(test);
System.out.print("DONE");
Menelaos
  • 23,508
  • 18
  • 90
  • 155
1

If you cannot change source code of class, you need to create Mixin class for omitting Studentlist ...

Follow below link for Jackson Mixin..

How can I tell jackson to ignore a property for which I don't have control over the source code?

Yogi
  • 1,805
  • 13
  • 24
0

You can use @JsonIgnoreProperties or @JsonIgnore for ignoring any property of class while JSON serialization. For e.g.

***public class StudentList {
int numberOfStudents;
int totalExpenditures;
@JsonIgnore
List<Student> students;
//getters setters
}***

Or

***@JsonIgnoreProperties(value = { "students" })
public class StudentList {
int numberOfStudents;
int totalExpenditures;
List<Student> students;
//getters setters
}***
Yogi
  • 1,805
  • 13
  • 24
  • I think you should read the comments under the question first. – Kh.Taheri Jul 28 '17 at 12:15
  • If you cannot change source code of class, you need to create Mixin class for omitting Studentlist ... Follow below link for Jackson Mixin.. https://stackoverflow.com/questions/7421474/how-can-i-tell-jackson-to-ignore-a-property-for-which-i-dont-have-control-over – Yogi Jul 28 '17 at 12:22
0

I believe from the question you cannot touch the StudentList class and everything should be handled from the StudentSummary class. What my idea is create another class SpecialStudent and override the setter/getter for the student list and then put the @JsonIgnore property on them. Below is the example for the same. Let me know if this helps

class SpecialStudent extends StudentList{
    @JsonIgnore
    @Override
    public List<Student> getStudents()
    {
        return students;
    }
    /**
     * @param students the students to set
     */
    @Override
    @JsonIgnore
    public void setStudents(List<Student> students)
    {
        this.students = students;
    }
}

Student Summary would look like this

public class StudentSummary
{
    SpecialStudent studentList;

    /**
     * @return the studentList
     */
    public StudentList getStudentList()
    {
        return studentList;
    }

    /**
     * @param studentList the studentList to set
     */
    public void setStudentList(SpecialStudent studentList)
    {
        this.studentList = studentList;
    }

}

Test class

StudentSummary sum = new  StudentSummary();
        SpecialStudent l = new SpecialStudent();
        l.setNumberOfStudents(4);
        l.setTotalExpenditures(12);
        sum.setStudentList(l);


        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(sum);

        System.out.println(json);

Output

{
  "studentList" : {
    "numberOfStudents" : 4,
    "totalExpenditures" : 12
  }
}
utkarsh31
  • 1,439
  • 2
  • 13
  • 20
0

I suggest reading here

Basically, if you cannot modify anything on the class itself, then you will need to tell jackson globally how to handle this on the serialization config (through the mixing annotations on the object mapper)

albert_nil
  • 1,648
  • 7
  • 9
-1

Since you could NOT change the StudentList; You do not need to include an instance of StudentList in StudentSummary. So, you could have some sort of mapping like the following:

 public class StudentSummary {

    int numberOfStudents;
    int totalExpenditures;

    public StudentSummary (){
         StudentList studentList = new StudentList();
         this.numberOfStudents = studentList.getNumberOfStudents();
         this.totalExpenditures = studentList.getTotalExpenditures();
    }

    //getters setters for numberOfStudents & totalExpenditures
  }
Kh.Taheri
  • 946
  • 1
  • 10
  • 25
  • The op was asking how to hide this field from the output. Removing it from the class definition would ofcourse solve the problem but it is not the solution that is being asked for. We want to keep the list in the java class. Sorry! – Menelaos Jul 28 '17 at 12:12
  • @MenelaosBakopoulos -- He said he can NOT update the StudentList class!! Have you read the comments under the question!! – Kh.Taheri Jul 28 '17 at 12:14
  • @Kh.Taheri You are correct, there should be NO modifications made to the StudentList.class. The solution you provided, doesn't really answer my question, because I wanted to know if there is a way with annotations to exclude the properties for the JSON output. In my real life scenario, I have about 9 different services, all of which will contain something, that I do not want from them (which so happen to have a list inside aswell) – Hekky TwoFiveEightNine Jul 28 '17 at 12:21