Want to hide some fields of an object that are being mapped to JSON by Jackson
If you don't want to put annotations on your Pojos you can also use Genson.
Here is how you can exclude a field with it without any annotations (you can also use annotations if you want, but you have the choice).
Genson genson = new Genson.Builder().exclude("securityCode", User.class).create();
// and then
String json = genson.serialize(user);
you also can gather all properties on an annotation class
@JsonIgnoreProperties( { "applications" })
public MyClass ...
String applications;
Adding this here because somebody else may search this again in future, like me. This Answer is an extension to the Accepted Answer
You have two options:
1. Jackson works on setters-getters of fields. So, you can just remove getter of field which you want to omit in JSON. ( If you don't need getter at other place.)
2. Or, you can use the `@JsonIgnore` [annotation of Jackson][1] on getter method of that field and you see there in no such key-value pair in resulted JSON.
@JsonIgnore
public int getSecurityCode(){
return securityCode;
}
Actually, newer version of Jackson added READ_ONLY and WRITE_ONLY annotation arguments for JsonProperty. So you could also do something like this.
@JsonProperty(access = Access.WRITE_ONLY)
private String securityCode;
instead of
@JsonIgnore
public int getSecurityCode(){
return securityCode;
}
You have two options:
Jackson works on setters-getters of fields. So, you can just remove getter of field which you want to omit in JSON. ( If you don't need getter at other place.)
Or, you can use the
@JsonIgnore
annotation of Jackson on getter method of that field and you see there in no such key-value pair in resulted JSON.@JsonIgnore public int getSecurityCode(){ return securityCode; }