@JsonInclude

/**
注解用来表示when value of the annotated property
(when used for a field, method or constructor parameter),
all properties of the annotated class, is to be serialized.
Without annotation property values are always included,but
by using this annotation one cat specify simple exclusion rules to reduce amount of properties to write out.
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD,
        ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotaton
public @interface JsonInclude{

        /**
        Inclusion rule to use for instances(values) of types (Classes) or 
        properties annotated.
        */
        public Include value() default Include.ALWAYS;

        /**
        Inclusion rule to use for entries ("content") of annotated
        {@link java.util.Map}; defaults to {@link Include#ALWAYS}.
        */
        public Include content() default Include.ALWAYS;

        /**
        与{@link JsonInclude}一起使用来定义Java Bean的哪些属性在序列化时被包括
        注意:在Jackson 1.x的JsonSerialize注解中有一个相同的命名为("Inclusion")的枚举类:现在被废弃
        而使用此值
        */
        public enum Include{
                /**
                 表示永远被包括,独立于属性的值       
                */
                ALWAYS,
                /**
                表示仅具有非null值的属性被包括
                */
                NON_NULL,
                /**
                表示属性的值与默认设置(当调用无参构造器时属性具有的值)的不一样时才被包括。
                此值不适用于{@link java.util.Map},因为他们没有默认值。如果使用了,与
                {@link #ALWAYS}一样
                */
                NON_DEFAULT,

                /**
                表示仅当属性值不是null并且不是被认为是空的值被包括
                定认的空值是数据类型特殊的;在实际的处理中参考下面的值

                默认的空值被像下面这样定义:
                对于{@link java.util.Collection}和{@link java.util.Map},调用其
                isEmpty()方法;

                对于Java数组,空数组的长度为0

                对于{@link java.lang.String},调用其length()方法,返回的值如果是0表示空字符串
                (注意String.isEmpty()在Java 1.6中被添加Jackson中没有用到)

                对于date/time类型,如果timestamp到是0(January 1st, 1970, UTC),value值被认为是空

                对于其它类型,null values are excluded but other exclusions(if any)

                注意默认的处理可以被自定义的JsonSerializer实现覆盖:如果isEmpty()方法被覆盖,it will
                be called to see if non-null values are considered empty(null is always considered empty。)
                */
                NON_EMPTY
        }
}

举例

results matching ""

    No results matching ""