Is there any way to prevent a field from deserialization in jackson? but i need to serialize that field I tried with @jsonIgnoreProperties this prevent both serialization and deserialization.
Asked
Active
Viewed 3,853 times
3 Answers
4
Starting with Jackson 2.6, a property can be marked as read- or write-only. It's simpler than hacking the annotations on both accessors and keeps all the information in one place:
public class NoDeserialization {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String prop;
public String getProp() {
return prop;
}
public void setProp(String prop) {
this.prop = prop;
}
}

Frank Pavageau
- 11,477
- 1
- 43
- 53
3
The "trick" is to combine the @JsonProperty and @JsonIgnore on the setters and getters, like in the following example
public class SerializeDemo{
@JsonIgnore
private String serializeOnly;
@JsonProperty("serializeOnly")
public String getSerializeOnly() {
return serializeOnly;
}
@JsonIgnore
public void setSerializeOnly(String serializeOnly) {
this.serializeOnly= serializeOnly;
}
}

Master Slave
- 27,771
- 4
- 57
- 55
-
Thanks. Will try and let you know. – virtualpathum Dec 01 '14 at 06:37
-
Yeah. It really does "The Trick". Thanks a lot – virtualpathum Dec 01 '14 at 07:44
-
Now we have another issue. We cannot use @JsonIgnore in both setter and the attribute. Our spring web project gives the following exception. org.springframework.data.mapping.model.MappingException: Ambiguous mapping! Annotation JsonIgnore configured on field yyyyyy and one of its accessor methods in class XXXXXEntity! Any Idea?? – virtualpathum Dec 02 '14 at 01:42
-
1Starting with Jackson 2.6 `@JsonProperty(access = JsonProperty.Access.READ_ONLY)` should do, see [Frank Pavageau's answer](https://stackoverflow.com/a/49321318/2032157) – Alexander Melnichuk Mar 26 '20 at 06:16
0
You can use @JsonIgnore
om field declaration.
ex.
@JsonIgnore
private Integer fieldToPrevent; // this will be avoided
private Integer regularField; // will serialize
look here's a explanation of it.

Community
- 1
- 1

user3145373 ツ
- 7,858
- 6
- 43
- 62