Object-to-json filtering in spring (jackson)

The json parser that comes with spring is jackson

jackson annotation

@JsonIgnore This annotation is used on properties to ignore the property when performing JSON operations.

@JsonFormat This annotation is used on properties to directly convert the Date type to the desired format, such as @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss").

@JsonProperty This annotation is used on properties to serialize the name of the property to another name, such as serializing the trueName property to name, @JsonProperty("name")

Object to Json to NULL or empty does not participate in serialization

  1. Annotation on the object @JsonInclude(Include.NON_NULL)  
    //Put this tag on the attribute, if the attribute is NULL, it will not participate in serialization''
    //If it is placed on the class, it will work on all attributes of this class 
    / /Include.Include.ALWAYS default 
    //Include.NON_DEFAULT property is default value not serialized 
    //Include.NON_EMPTY property is empty ("") or NULL not serialized 
    //Include.NON_NULL property is NULL not serialized 
  2. Code processing ( Note: only works on objects, Map and List do not work )
      User user=new User();
        user.setId("111");
        user.setCreateDate(new Date());
        user.setCreateBy(null);
        ObjectMapper mapper=new ObjectMapper();
        mapper.setSerializationInclusion(Include.ALWAYS);
        String outJson=mapper.writeValueAsString(user);
        System.out.println(outJson);

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846815&siteId=291194637