I get this Json response:
{
"student": {
"firstName": "john",
"lastName": "doe"
"isActive": false
}
Now, I don’t want to include isActive from the response. I have this class ClassDto:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = { "student.isActive" })
public class ClassDto implements Serializable
{
private StudentDto student;
public StudentDto getStudent()
{
return student;
}
public void setStudent(StudentDto student)
{
this.student = student;
}
}
Here is also my StudentDto class:
@JsonInclude(Include.NON_NULL)
public class StudentDto implements Serializable
{
@JsonProperty("firstName")
private String firstName;
@JsonProperty("lastName")
private String lastName;
@JsonProperty("isActive")
private boolean isActive;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public boolean isActive()
{
return isActive;
}
public void setIsActive(boolean isActive)
{
this.isActive = isActive;
}
}
I created a mapper class ClassMapper:
public class ClassMapper
{
private final StudentVo studentVo
public ClassMapper(StudentVo studentVo)
{
this.studentVo = studentVo;
}
public ClassDto mapToResource() throws VerifyException
{
final ClassDto classDto = new ClassDto();
StudentDto studentDto = new StudentDto();
extractStudent(studentDto, studentVo);
classDto.setStudent(studentDto);
return classDto;
}
private void extractStudent(StudentDto studentDto,
StudentVo studentVo)
{
studentDto.setFirstName(studentVo.getFirstName());
studentDto.setLastName(studentVo.getLastName());
}
}
I am currently reusing an existing class StudentDto in my ClassDto since it has the fields that I need except that I don’t want ‘isActive’ field to show in the Json response. As you can see, I have added @JsonIgnoreProperties(value = { "student.isActive" })
in my ClassDto and unfortunately, this is not working. I tested adding the @JsonIgnore in StudentDto.isActive field and it works. However, since this class is being used by other classes, I don’t want to change the codes in this class. What is wrong with my code? How can I exclude the isActive field from my Json response?