Jackson serialization and de-serialization ignored field

Transfer: https://blog.csdn.net/yu870646595/article/details/78523402

First, when the set of sequences comprising only Jackson not empty field

new ObjectMapper().setSerializationInclusion(Include.NON_NULL);

Second, disposed deserializing JSON string present in negligible, and does not exist in Java properties

new ObjectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

Three, Jackson ignored the way the field when serializing

1, a way: FilterProvider

a) ignore the need to add @JsonFilter ( "fieldFilter" on some bean fields)

b) ObjectMapper filter disposed

   FilterProvider filterProvider = new SimpleFilterProvider();

   SimpleBeanPropertyFilter fieldFilter = SimpleBeanPropertyFilter().serializeAllExcept("name");
   filterProvider.addFilter("fieldFilter");

   new ObjectMapper.setFilters(filterProvider );

2. Second way: Using @JsonIgnore

   Notes @JsonIgnore marked on the field to be ignored when the sequence of the fields can be ignored

Transfer: https://blog.csdn.net/ngl272/article/details/70217104

Jackson used to solve very easy, just on the annotate entity classes can be.

@JsonIgnoreProperties(ignoreUnknown = true)
class ExtraBean {
    private boolean is_museuser;
 
    public boolean isIs_museuser() {
        return is_museuser;
    }
 
    public void setIs_museuser(boolean is_museuser) {
        this.is_museuser = is_museuser;
    }
}

@JsonIgnore annotations to ignore certain fields, or can be used in Field Getter method, the method is used in a Setter, and Filed same effect. This comment can only be used in the presence of a POJO fields to be ignored, we can not meet the needs of the situation now.

@JsonIgnoreProperties (ignoreUnknown = true), this field annotations written after the class, the class will be ignored does not exist to meet current needs. This annotation can also specify the fields to be ignored. Use as follows:

@JsonIgnoreProperties ({ "internalId", " secretKey"})
specified field will not be serialized and deserialized.

Guess you like

Origin blog.csdn.net/Dongguabai/article/details/89735187