json別名

面對不遵照駝峯命名規則的接口咋辦?固然首先要吐槽一下,不過接口是別人定的,雖然看着不爽但仍是得去適配,好比cardNumber,他返回的叫{CARDNUMBER:''}html

@JsonAutoDetect(JsonMethod.FIELD)  
public class MemberApiParameter implements Serializable {  
  
    private static final long serialVersionUID = 1L;  
  
    /** 姓名 **/  
    @JsonProperty("NAME")  
    private String name;  
  
    /** 性別 **/  
    @JsonProperty("SEX")  
    private String sex;  
  
        public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public String getSex() {  
        return sex;  
    }  
  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
}

經過對API的研究能夠經過@JsonProperty以及@JsonAutoDetect來實現。java

@sonProperty("NAME")顧名思義,就是顯示指定字段的別名,無論是輸入仍是輸出都是這個名字。api

 @JsonAutoDetect(JsonMethod.FIELD)這個的意思是指解析字段,若是不這樣設置,有興趣的朋友能夠試一下,會輸出兩套東西,相似{name:'',NAME:''},也就是說字段和getter方法都解析了,因此須要制定只解析字段名,忽略方法。還有一種方法就是須要一行行的在全部getter上加上@JsonIgnore,若是字段多就累死了。oracle

JsonMethod的API說明:this

Enum Constant Summaryspa

ALL 
          This pseudo-type indicates that all of real types are included
CREATOR 
          Creators are constructors and (static) factory methods used to construct POJO instances for deserialization
FIELD 
          Field refers to fields of regular Java objects.
GETTER 
          Getters are methods used to get a POJO field value for serialization, or, under certain conditions also for de-serialization.
IS_GETTER 
          "Is getters" are getter-like methods that are named "isXxx" (instead of "getXxx" for getters) and return boolean value (either primitive, or Boolean).
NONE 
          This pseudo-type indicates that none of real types is included
SETTER            Setters are methods used to set a POJO value for deserialization.
相關文章
相關標籤/搜索