jackson-annotations之@JsonInclude、@JsonPropertyOrder、@JsonSerialize

  In the actual development process, to be more elegant interact front and rear ends, the front end sometimes only non-empty field, and want to have certain meanings, and converting the order of the enumeration type field (e.g.: a number stored in a database field , expected to return to the front end is the meaning of this figure)

solution:

1), the entity class filtration using @JsonInclude empty field

2), the entity class used to sort fields @JsonPropertyOrder

3), used in the field and need to be converted @JsonSerialize json custom serialization, if using Mybatis-plus frame there is another solution, the next chapter Demo

Import com.fasterxml.jackson.annotation.JsonFormat;
 Import com.fasterxml.jackson.annotation.JsonInclude;
 Import com.fasterxml.jackson.annotation.JsonPropertyOrder;
 Import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 Import com.sand .base.util.json.serializer.DataScopeSerializer; Import java.util.Date; 

/ ** 
 * function: system roles 
 * developer: @author liusha 
 * development date: 2019/8/30 13:10 
 * functional Description: system role 
 * / 
@JsonInclude (JsonInclude.Include.NON_NULL) 
@JsonPropertyOrder ({ "roleId", "roleName", "roleKey", "orderNum", "Status", "delflag", "dataScope"})
public  class SysRole {
   Private  static  Final  Long serialVersionUID = 9084971943250909716L ;
   / ** 
   * creator 
   * / 
  Private String createBy;
   / ** 
   * updater 
   * / 
  Private String updateBy;
   / ** 
   * creation time 
   * / 
  @JsonFormat (pattern = " the mM-dd-HH YYYY: mm: SS " )
   Private a Date createTime; 

  / ** 
   * update 
   * / 
  @JsonFormat (pattern =" the mM-dd-YYYY HH: mm: SS " )
   Private a Date updateTime; 

  / ** 
   * Remarks information 
   * /
  Private String the remark;
   / ** 
   * role ID 
   * * / 
  Private String roleId;
   / ** 
   * Role name 
   * * / 
  Private String roleName;
   / ** 
   * authority string 
   * * / 
  Private String roleKey;
   / ** 
   * display order 
   * * / 
  Private  int orderNum;
   / ** 
   * the role of the state (0 to disable the normal 1) 
   * * / 
  Private String status;
   / ** 
   * delete flag (0 logic 1 is not deleted logically deleted) 
   * * / 
  Private String delflag;
   / **
   * Data range (1: 2 all the data permissions: Custom Data permissions 3: 4 in this sector permission data: the following data in this sector and permissions) 
   * * / 
  @JsonSerialize (the using . DataScopeSerializer = class )
   Private String Datascope; 

 / omitted ** get, set method * / }

Json custom serialization DataScopeSerializer.class

Import com.fasterxml.jackson.core.JsonGenerator;
 Import com.fasterxml.jackson.databind.JsonSerializer;
 Import com.fasterxml.jackson.databind.SerializerProvider; 

Import java.io.IOException; 

/ ** 
 * Function: character data range json serialization 
 * developer: @author liusha 
 * development date: 2019/9/1 18:24 
 * function: role json serialization data range 
 * / 
public  class DataScopeSerializer the extends JsonSerializer <String> { 
  @Override 
  public  void the serialize (String Datascope, jsonGenerator jsonGenerator, serializerProvider serializerProvider) throws IOException {
    String dataScopeStr; 
    Switch (Datascope) {
       Case ". 1" : 
        dataScopeStr = "all data authority" ;
         BREAK ;
       Case "2" : 
        dataScopeStr = "custom data authority" ;
         BREAK ;
       Case ". 3" : 
        dataScopeStr = "department data rights " ;
         BREAK ;
       Case " 4 " : 
        dataScopeStr =" of the department and the following data rights " ;
         BREAK ;
       default :
        dataScopeStr = "no such data authority";
    }
    jsonGenerator.writeString(dataScopeStr);
  }
}

Effect after processing is as follows:

Guess you like

Origin www.cnblogs.com/54hsh/p/11443135.html