@JsonIgnore with @Getter Annotation

Prateek Jain picture Prateek Jain · Jun 28, 2014 · Viewed 19.6k times · Source

Can i use @JsonIgnore with @Getter annotation from lombok without explicitly define the getter, because i have to use this JsonIgnore while serializing the object but while deserializing, the JsonIgnore annotation must be ignore so the field in my object must not be a null.

@Getter
@Setter
public class User {

    private userName;

    @JsonIgnore
    private password;
}

i know, just by define the JsonIgnore on the getter of password i can prevent my password to be serialized but for that i have to explicitly define the getter thing that i don't want. Any idea please, Any help will be appreciated.

Answer

Sebastian picture Sebastian · Dec 30, 2014

To put the @JsonIgnore to the generated getter method, you can use onMethod = @__( @JsonIgnore ). This will generate the getter with the specific annotation. For more details check http://projectlombok.org/features/GetterSetter.html

@Getter
@Setter
public class User {

    private userName;

    @Getter(onMethod = @__( @JsonIgnore ))
    @Setter
    private password;
}