@JsonIgnore的源碼說明

來源:互聯網
上載者:User

標籤:

@JsonIgnore不僅僅是在getter上有影響,也對setter方法有影響:

所在包:com.fasterxml.jackson.annotation;

源碼:

import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * Marker annotation that indicates that the annotated method or field is to be//標記方法或欄位 * ignored by introspection-based//被標記的方法和欄位不進行序列化和還原序列化 * serialization and deserialization functionality. That is, it should * not be consider a "getter", "setter" or "creator".//實際上就是:這個註解不能被認為是一個getter,setter或creator *<p> * In addition, starting with Jackson 1.9, if this is the only annotation//1.9版本如果僅加入此註解,將會導致整個屬性被忽視 * associated with a property, it will also cause the whole * property to be ignored: that is, if setter has this annotation and//如果在setter上註解,在getter上不註解,那麼getter也會受影響(將不會被序列化) * getter has no annotations, getter is also effectively ignored. * It is still possible for different accessors to use different//不同的存取器可以用不同的註解 * annotations; so if only "getter" is to be ignored, other accessors//如果僅是getter加了此註解,那麼其他的存取器(setter或欄位) * (setter or field) would need explicit annotation to prevent//需要明確地被註解以防止不被序列化(通常使用@JsonProperty來註解) * ignoral (usually {@link JsonProperty}). * <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> * Before version 1.9, this annotation worked purely on method-by-method (or field-by-field) * basis; annotation on one method or field did not imply ignoring other methods * or fields. However, with version 1.9 and above, annotations associated * with various accessors (getter, setter, field, constructor parameter) of * a logical property are combined; meaning that annotations in one (say, setter) * can have effects on all of them (if getter or field has nothing indicating * otherwise). *<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. *<p> * Annotation is similar to {@link javax.xml.bind.annotation.XmlTransient}  */@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)@JacksonAnnotationpublic @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;}

 

JsonIgnore可以標註在欄位、方法上(1.9版本及以上),當註解@JsonIgnore時,會發生兩件事:

①:不會被jackson解析;

②:欄位或方法所屬的類不會成為持久化類

@JsonIgnore的源碼說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.