简体   繁体   中英

Is there any way to prevent field from deserialization in jackson?

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.

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;
      }
    }

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;
    }
}

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.

I have gave same answer here also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM