简体   繁体   English

有什么方法可以防止杰克逊场反序列化吗?

[英]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. 但是我需要序列化我尝试使用@jsonIgnoreProperties的字段,以防止序列化和反序列化。

The "trick" is to combine the @JsonProperty and @JsonIgnore on the setters and getters, like in the following example “技巧”是将setter和getter上的@JsonProperty和@JsonIgnore结合起来,如以下示例所示

 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. Jackson 2.6开始,可以将属性标记为只读或只写。 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. 您可以使用@JsonIgnore om字段声明。

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. 我在这里也给出了相同的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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