本文為博主原創,未經允許不得轉載:
最近用的比較多,把json相關的知識點都總結一下,jackjson的注解使用比較頻繁,
jackson的maven依賴
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency>
在這單獨總結一下,最近常用到的注解。
1.@JsonProperty :此注解用於屬性上,作用是把該屬性的名稱序列化為另外一個名稱,如把trueName屬性序列化為name,@JsonProperty("name")。
對屬性名稱重命名,比如在很多場景下Java對象的屬性是按照規范的駝峰書寫,但在數據庫設計時使用的是下划線連接方式,此處在進行映射的時候
就可以使用該注解。
例如:使用該注解將以下表結構轉化為Javabean:
public class CustomerInfo{ private int id;
//使用 @JsonProperty注解將表結構中的字段映射到實體類中 @JsonProperty("customer_name") private String customerName; @JsonProperty("customer_id") private String customerId; @JsonProperty("product_id") private String productId; @JsonProperty("source_address") private String sourceAddress; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getCustomerId() { return customerId; } public void setCustomerId(String customerId) { this.customerId = customerId; } public String getProductId() { return productId; } public void setProductId(String productId) { this.productId = productId; } public String getSourceAddress() { return sourceAddress; } public void setSourceAddress(String sourceAddress) { this.sourceAddress = sourceAddress; } }
2.@JsonIgnore此注解用於屬性或者方法上(最好是屬性上),用來完全忽略被注解的字段和方法對應的屬性,即便這個字段或方法可以被自動檢測到或者還有其
他的注解,一般標記在屬性或者方法上,返回的json數據即不包含該屬性。
使用情景:需要把一個List<CustomerInfo >轉換成json格式的數據傳遞給前台。但實體類中基本屬性字段的值都存儲在快照屬性字段中。此時我可以在業務層中做處理,
把快照屬性字段的值賦給實體類中對應的基本屬性字段。最后,我希望返回的json數據中不包含這兩個快照字段,那么在實體類中快照屬性上加注解@JsonIgnore,
那么最后返回的json數據,將不會包含customerId和productId兩個屬性值。
public class CustomerInfo { private int id; //使用 @JsonIgnore注解在生成json數據時,忽略該字段 private String customerName; @JsonIgnore private String customerId; @JsonIgnore private String productId; private String sourceAddress; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getCustomerId() { return customerId; } public void setCustomerId(String customerId) { this.customerId = customerId; } public String getProductId() { return productId; } public void setProductId(String productId) { this.productId = productId; } public String getSourceAddress() { return sourceAddress; } public void setSourceAddress(String sourceAddress) { this.sourceAddress = sourceAddress; } }
3.@JsonIgnoreProperties此注解是類注解,作用是json序列化時將java bean中的一些屬性忽略掉,序列化和反序列化都受影響。
4.@JsonFormat此注解用於屬性或者方法上(最好是屬性上),可以方便的把Date類型直接轉化為我們想要的模式。
例子:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date updateTime;
5.@JsonSerialize此注解用於屬性或者getter方法上,用於在序列化時嵌入我們自定義的代碼,比如序列化一個double時在其后面限制兩位小數點。
6.@JsonDeserialize此注解用於屬性或者setter方法上,用於在反序列化時可以嵌入我們自定義的代碼,類似於上面的@JsonSerialize。
7.@JsonInclude 屬性值為null的不參與序列化。例子:@JsonInclude(Include.NON_NULL)