Below is the sample class :
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Data
@JsonInclude(Include.NON_NULL)
public class SpouseRes implements Serializable {
private static final long serialVersionUID = 7830471917333093173L;
//This object is used to check if the object is empty or not (all data members are null)
@JsonIgnore
private static final SpouseRes EMPTY = new SpouseRes();
@JsonInclude(Include.NON_DEFAULT)
@JsonProperty("age")
private Integer age;
@JsonProperty("salary")
private SalaryResponse salary;
// Function to check if the current object is empty
@JsonIgnore
public boolean isEmpty() {
return this.equals(EMPTY);
}
}
When getting this object from response, this is empty and the data member is getting set to 0. Thats why its not getting detected as empty in Object.isEmpty(). Below is the setter code :
// Setting spouse salary
SpouseRes spouse = new SpouseRes();
// Check if Retirement age is null or default value 0
if(Client.getSpouseRetireAge() != 0 || Client.getSpouseRetireAge() != null) {
spouse.setRetireAge(Client.getSpouseRetireAge());
}
else {
spouse.setRetireAge(null);
}
The JSON output is :
"client": {
"displayName": "Tony",
"age": 14,
"retirementAge": 90,
"salary": {
"amount": 0.0
},
"spouse": {}
}
So, after using all of these, still getting this empty Object in JSON response, How to remove this ?