@JsonInclude注解的使用
解释
@JsonInclude注解的作用是指定实体类在序列化时的策略
用法
@JsonInclude注解用在实体类上,可配合一些参数来使用,例如
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
// ...
}
我们来看看具体的参数枚举类
public enum Include
{
/**
* Value that indicates that property is to be always included,
* independent of value of the property.
*/
ALWAYS,
/**
* Value that indicates that only properties with non-null
* values are to be included.
*/
NON_NULL,
/**
* Value that indicates that properties are included unless their value
* is:
*<ul>
* <li>null</li>
* <li>"absent" value of a referential type (like Java 8 `Optional`, or
* {link java.utl.concurrent.atomic.AtomicReference}); that is, something
* that would not deference to a non-null value.
* </ul>
* This option is mostly used to work with "Optional"s (Java 8, Guava).
*
* @since 2.6
*/
NON_ABSENT,
/**
* Value that indicates that only properties with null value,
* or what is considered empty, are not to be included.
* Definition of emptiness is data type specific; see below
* for details on actual handling.
*<p>
* Default emptiness for all types includes:
*<ul>
* <li><code>Null</code> values.</li>
* <li>"Absent" values (see {@link #NON_ABSENT})</li>
*</ul>
* so that as baseline, "empty" set includes values that would be
* excluded by both {@link #NON_NULL} and {@link #NON_ABSENT}.
*<br>
* Beyond this base, following types have additional empty values:
*<ul>
* <li>For {@link java.util.Collection}s and {@link java.util.Map}s,
* method <code>isEmpty()</code> is called;
* </li>
* <li>For Java arrays, empty arrays are ones with length of 0
* </li>
* <li>For Java {@link java.lang.String}s, <code>length()</code> is called,
* and return value of 0 indicates empty String
* </li>
* </ul>
* and for other types, null values are excluded but other exclusions (if any).
*<p>
* Note that this default handling can be overridden by custom
* <code>JsonSerializer</code> implementation: if method <code>isEmpty()</code>
* is overridden, it will be called to see if non-null values are
* considered empty (null is always considered empty).
*<p>
* Compatibility note: Jackson 2.6 included a wider range of "empty" values than
* either earlier (up to 2.5) or later (2.7 and beyond) types; specifically:
*<ul>
* <li>Default values of primitive types (like <code>0</code> for `int`/`java.lang.Integer`
* and `false` for `bool`/`Boolean`)
* </li>
* <li>Timestamp 0 for date/time types
* </li>
*</ul>
* With 2.7, definition has been tightened back to only containing types explained
* above (null, absent, empty String, empty containers), and now
* extended definition may be specified using {@link #NON_DEFAULT}.
*/
NON_EMPTY,
/**
* Meaning of this setting depends on context: whether annotation is
* specified for POJO type (class), or not. In latter case annotation
* is either used as the global default, or as property override.
*<p>
* When used for a POJO, definition is that only values that differ from
* the default values of POJO properties are included. This is done
* by creating an instance of POJO using zero-argument constructor,
* and accessing property values: value is used as the default value
* by using <code>equals()</code> method, except for the case where property
* has `null` value in which case straight null check is used.
*<p>
* When NOT used for a POJO (that is, as a global default, or as property
* override), definition is such that:
*<ul>
* <li>All values considered "empty" (as per {@link #NON_EMPTY}) are excluded</li>
* <li>Primitive/wrapper default values are excluded</li>
* <li>Date/time values that have timestamp (`long` value of milliseconds since
* epoch, see {@link java.util.Date}) of `0L` are excluded</li>
* </ul>
*/
NON_DEFAULT,
/**
* Value that indicates that separate `filter` Object (specified by
* {@link JsonInclude#valueFilter} for value itself, and/or
* {@link JsonInclude#contentFilter} for contents of structured types)
* is to be used for determining inclusion criteria.
* Filter object's <code>equals()</code> method is called with value
* to serialize; if it returns <code>true</code> value is <b>excluded</b>
* (that is, filtered out); if <code>false</code> value is <b>included</b>.
*
* @since 2.9
*/
CUSTOM,
/**
* Pseudo-value used to indicate that the higher-level defaults make
* sense, to avoid overriding inclusion value. For example, if returned
* for a property this would use defaults for the class that contains
* property, if any defined; and if none defined for that, then
* global serialization inclusion details.
*
* @since 2.6
*/
USE_DEFAULTS
;
}
- ALWAYS:表示总是序列化所有属性
- NON_NULL:表示序列化非null属性
- NON_ABSENT:表示序列化非null或者引用类型缺省值,例如java8的Optional类,这个选中通常与Optional一起使用
- NON_EMPTY:表示序列化非Empty的属性,例如空的集合不会被序列化
- NON_DEFAULT:仅包含与POJO属性默认值不同的值
- CUSTOM:由{@link JsonInclude#valueFilter}指定值本身,或由{@link JsonInclude#contentFilter}指定结构化类型的内容,由过滤器对象的equals方法进行序列化,返回true则会被排除,返回false会被序列化
- USE_DEFAULTS:使用默认值
代码示例
entity
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
private Long id;
private String name;
private Integer sex;
// ... 此处省略gettet setter
}
controller
// ... 省略
@GetMapping("/get")
@ResonseBody
public User get(){
User user = new User();
user.setName("张三");
return user
}
// ... 省略
返回结果
{
"name":"张三"
}