Reputation: 1552
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 ignored so the field in my object must not be 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.
Upvotes: 32
Views: 34531
Reputation: 938
This could be quite obvious but I lost a lot of time not thinking this solution before:
@Getter
@Setter
public class User {
private userName;
private password;
@JsonIgnore
public getPassword() { return password; }
}
As Sebastian said @__( @JsonIgnore )
can resolve this issue but sometimes the use of the onX Lombok feature (@__()) can have side-effects for example breaking the javadoc generation.
Upvotes: 4
Reputation: 4146
I recently had the same issue.
There are several ways to solve it:
lombok.config
in the root folder of your project with content:// says that it's primary config (lombok will not scan other folders then)
config.stopBubbling = true
// forces to copy @JsonIgnore annotation on generated constructors / getters / setters
lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonIgnore
...
and in your class you can use this annotation as usual, on the field level:
@JsonIgnore
private String name;
Note: if you use lombok @RequiredArgsConstructor or @AllArgsConstructor, then you should remove all usages of @JsonIgnore
with @JsonIgnoreProperties
(as described in solution #4, or you may still choose solution #2 or #3). This is required because @JsonIgnore
annotation is not applicable for constructor arguments.
@JsonIgnore
annotation on them:@JsonIgnore
public String getName() { return name; }
@JsonIgnore
public void setName(String name) { this.name = name; }
@JsonProperty
(it's either read-only or write-only, but no both):@JsonProperty(access = JsonProperty.Access.READ_ONLY) // will be ignored during serialization
private String name;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) // will be ignored during deserialization
private String name;
@JsonIgnoreProperties({ "fieldName1", "fieldName2", "..."})
I personally use solution #1 globally and solution #4 for exceptions when class also has annotations @AllArgsConstructor
or @RequiredArgsConstructor
.
Upvotes: 1
Reputation: 4805
With JDK version 8 use below:
// @Getter(onMethod=@__({@Id, @Column(name="unique-id")})) //JDK7
// @Setter(onParam=@__(@Max(10000))) //JDK7
@Getter(onMethod_={@Id, @Column(name="unique-id")}) //JDK8
@Setter(onParam_=@Max(10000)) //JDK8
Source : https://projectlombok.org/features/experimental/onX
Upvotes: 0
Reputation: 195
Recently i had the same issue using jackson-annotation 2.9.0 and lombok 1.18.2
This is what worked for me:
@Getter
@Setter
public class User {
@JsonIgnore
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
So basically adding the annotation @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
means that the property may only be written for deserialization (using setter) but will not be read on serialization (using getter)
Upvotes: 15
Reputation: 776
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;
}
Upvotes: 57