Exclude data member in response json file

Viewed 280

I am getting unwanted data field in my Json response file. I tried with the @JsonIgnore, but still getting the unwanted field in the output. Following is the model class :

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
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 RToleranceResponse implements Serializable  {
    
    //This object is used to check if the object is empty or not (all data members are null)
    @JsonIgnore
    private static final RToleranceResponse EMPTY = new RToleranceResponse();
    
    private static final long serialVersionUID = -8284498145392258180L;
    private String description;
    private Double totalMean;
    private Double Rmrisk;
    private Double upsideHm;

    
    // Function to check if the current object is empty
    public boolean isEmpty() {
        return this.equals(EMPTY);
    }

}

Json output on Postman :

"tolerance": {
      "description": "Very conservative",
      "empty": false
}

Don't want this EMPTY variable to be there. Where EMPTY variable is use to check whether the entire object is null or not(as I am using new keyword). This is just a sample class and output to keep the question small. So how to exclude the data fields form the response Json file.

1 Answers

This empty in JSON response is coming from public method isEmpty(). Add the @JsonIgnore to that public method also to remove it from JSON response.

Related