I have a bean which I am returning as part of one of my controller methods. There are few fields in the bean that I have processing but do not want them to be returned back to the consumer. Is there a way to exclude some of the fields from being returned without having to create a new bean that only has the elements that I need to return?
Spring boot by default uses Jackson
to serialise/deserialise json
. In Jackson
, you can exclude a field by annotating it with @JsonIgnore
, e.g.
public class Bean {
@JsonIgnore
private String field1;
private String field2
//getters and setters
}
By doing this, field1
of Bean
class will not get sent in the response. Also, if this bean is used in the request, this field will not be deserialised from request payload.
Here's the documentation for JsonIgnore
.