(七)json序列化
在spring boot專案中已經包含有json序列化的框架,具體在包com.fasterxml.jackson.annotation中,建議看看詳細原始碼。
但在專案應用上還是會有一些坑會出現的,舉個例子:
在一個複雜的業務模型中包含有200個欄位,在查詢列表時只查詢其中某20個欄位,在查詢詳情中需要把所有欄位都查詢出來。
一般情況下,如果是開始做一個新功能,那麼我們的設計應該類似是這樣的:
model
---- QueryModel ,包含20個欄位,響應查詢列表結果
---- DetailModel extend POJO , 包含所有欄位,響應查詢實體結果
entity
---- POJO,包含所有欄位
但維護從來都是一件蛋疼的事情,200個欄位是迭代出來的,他們的邏輯是這樣的:
entity
---- POJO,包含所有欄位,響應查詢列表和查詢實體結果
這時候會發現一切蛋疼的原因就是直接把pojo拿來當model用了,導致所有引數和結果無法拓展。為什麼會這麼蛋疼呢?原因不重要,如何解決才重要。
方案1:程式碼解耦,改造成model和entity分離
此方案的好處是一勞永逸,後續的拓展也比較輕鬆,弊端也顯而易見,會發費許多時間去理解業務重構業務,而且一個穩定的系統一般不會嘗試大範圍的改造,萬一改後出現一堆bug呢?
方案2:把不輸出到頁面的欄位忽略掉
這時候用到註解 @JsonIgnore,該註解既可以作用在欄位上也可以作用在方法上(另外兩種先不說),可以看看原始碼和註釋:
package com.fasterxml.jackson.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;View Code/** * Marker annotation that indicates that the logical property that * the accessor (field, getter/setter method or Creator parameter * [of {@link JsonCreator}-annotated constructor or factory method]) * is to be ignored by introspection-based * serialization and deserialization functionality. *<p> * Annotation only needs to be added to one of the accessors (often * getter method, but may be setter, field or creator parameter), * if the complete removal of the property is desired. * However: if only particular accessor is to be ignored (for example, * when ignoring one of potentially conflicting setter methods), * this can be done by annotating other not-to-be-ignored accessors * with {@link JsonProperty} (or its equivalents). This is considered * so-called "split property" case and allows definitions of * "read-only" (read from input into POJO) and "write-only" (write * in output but ignore on output) *<br> * NOTE! As Jackson 2.6, there is a new and improved way to define * `read-only` and `write-only` properties, using * {@link JsonProperty#access()} annotation: this is recommended over * use of separate <code>JsonIgnore</code> and {@link JsonProperty} * annotations. *<p> * For example, a "getter" method that would otherwise denote * a property (like, say, "getValue" to suggest property "value") * to serialize, would be ignored and no such property would * be output unless another annotation defines alternative method to use. *<p> * When ignoring the whole property, the default behavior if encountering * such property in input is to ignore it without exception; but if there * is a {@link JsonAnySetter} it will be called instead. Either way, * no exception will be thrown. *<p> * Annotation is usually used just a like a marker annotation, that * is, without explicitly defining 'value' argument (which defaults * to <code>true</code>): but argument can be explicitly defined. * This can be done to override an existing `JsonIgnore` by explicitly * defining one with 'false' argument: either in a sub-class, or by * using "mix-in annotations". */ @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotation public @interface JsonIgnore { /** * Optional argument that defines whether this annotation is active * or not. The only use for value 'false' if for overriding purposes * (which is not needed often); most likely it is needed for use * with "mix-in annotations" (aka "annotation overrides"). * For most cases, however, default value of "true" is just fine * and should be omitted. */ boolean value() default true; }
因此在使用的時候需要注意,如果只需要在查詢的時候忽略,在儲存的時候不忽略,那麼需要在getter方法上註解@JsonIgnore,在setter方法上註解 @JsonProperty,(這時候如果使用的lombok就很尷尬了),舉個例子:
public class TestEntity{private String name; /** * getter */ @JsonIgnore public String getName() { return name; } /** * setter */ @JsonProperty public void setName(String name) { this.name= name; } }
然後在sql上從select *改成select 指定欄位,解決查詢列表問題。
然而,在查詢單個實體詳情的時候這些被忽略的欄位也無法傳到前端了,這是一條乍一看很理想的死衚衕。
但從sql的角度上可以看出select 指定欄位的時候,在反序列化到pojo的時候其他欄位是沒有值的,那麼可以把方案換一下,讓有值得欄位序列化出去,沒有值的不序列化
方案3:讓有值得欄位序列化出去,沒有值的不序列化
把sql上從select *改成select 指定欄位
在pojo裡用上@JsonInclude(JsonInclude.Include.NON_NULL),
此時既滿足查詢列表,又滿足查詢詳情,解決問題。