Supun Wijerathne
Supun Wijerathne

Reputation: 12958

Hide negative values from a json by jackson?

I am developing a REST API. In one of my endpoints, my model (pojo) looks like this:

public class Tax{
    String name;
    Float value;
}

Anyway, I want to hide the field 'value' in my JSON response, if it's negative (-1 in my case). Is there any jackson annotation for this purpose or any other workaround?

Upvotes: 2

Views: 390

Answers (1)

Supun Wijerathne
Supun Wijerathne

Reputation: 12958

I finally found the most sensible answer.

  • add @JsonInclude(JsonInclude.Include.NON_DEFAULT). import this -> import com.fasterxml.jackson.annotation.JsonInclude;
  • make the default value of that attribute to -1.

    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    
    public class Tax{ 
        String name; 
        Float value = -1; 
    }
    

Upvotes: 3

Related Questions