@JsonRootName

用于在对象外包裹一层对象

特性

  • 序列化:
    • 需要设置WRAP_ROOT_VALUE属性,jackson才会支持root对象,JsonRootName注解才会发挥作用

  • 反序列化:
    • 默认不支持对象名称,需要设置UNWRAP_ROOT_VALUE开启
    • 标注有注解的,则对象名称必须要保持一致才能反序列化成功

@JsonInclude

该注解的作用仅在序列化操作时有用:用于控制方法、属性等是否应该被序列化;
可以在类,属性,方法上进行使用

属性

ALWAYS

默认策略,任何情况都执行序列化
无论是空【null】还是空字符串【””】还是空数组【[]】都会进行序列化

  1. //@JsonInclude(JsonInclude.Include.ALWAYS
  2. {
  3. "id" : 1,
  4. "name" : "",
  5. "age" : null,
  6. "isAdmin" : true,
  7. "optional" : {
  8. "present" : false
  9. },
  10. "atomic" : null,
  11. "hobby" : [ ]
  12. }

NON_NULL

所有null的值都不会进行序列化,但是空字符串会被序列化

  1. //@JsonInclude(JsonInclude.Include.NonNull
  2. {
  3. "id" : 1,
  4. "name" : "",
  5. "isAdmin" : true,
  6. "optional" : {
  7. "present" : false
  8. },
  9. "atomic" : null,
  10. "hobby" : [ ]
  11. }

NON_ABSENT

当实例化的对象有Optional或AtomicReference类型的成员变量时,
如果Optional引用的实例和AtomicReference为空,用NON_ABSENT能使该字段不做序列化;

  1. //@JsonInclude(JsonInclude.Include.NonAbsent
  2. {
  3. "id" : 1,
  4. "name" : "",
  5. "isAdmin" : true,
  6. "hobby" : [ ]
  7. }

NON_EMPTY

以下情况都不会序列化

  1. null
  2. 空字符串
  3. 空集合
  4. 空数组
  5. Optional类型的,其引用为空
  6. AtomicReference类型的,其引用为空
  1. {
  2. "id" : 1,
  3. "isAdmin" : true,
  4. "optional" : {
  5. "present" : false
  6. }
  7. }

NON_DEFAULT

对保持默认值的字段不做序列化

  1. //@JsonInclude(JsonInclude.Include.NON_DEFAULT
  2. {
  3. "id" : 1,
  4. "name" : "",
  5. "optional" : {
  6. "present" : false
  7. },
  8. "atomic" : null,
  9. "hobby" : [ ]
  10. }

CUSTOM

此时要指定valueFilter属性,该属性对应一个类,用来自定义判断被JsonInclude修饰的字段是否序列化

  1. //@JsonInclude(JsonInclude.Include.CUSTOM
  2. {
  3. "name" : ""
  4. }

USE_DEFAULTS

当JsonInclude在类和属性上都有时,
优先使用属性上的注解,此时如果在序列化的属性的get方法上使用了JsonInclude,并设置为USE_DEFAULTS,就会使用类注解的设置
通常该配置用于全局搭配局部进行使用

  1. //@JsonInclude(JsonInclude.Include.USE_DEFAULTS
  2. {
  3. "id" : 1,
  4. "name" : "",
  5. "age" : null,
  6. "isAdmin" : true,
  7. "optional" : {
  8. "present" : false
  9. },
  10. "atomic" : null
  11. }


测试类

实体类

  1. //@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
  2. //@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = CustomFilter.class)
  3. //@JsonInclude(JsonInclude.Include.NON_DEFAULT)
  4. //@JsonInclude(JsonInclude.Include.NON_EMPTY)
  5. //@JsonInclude(JsonInclude.Include.NON_ABSENT)
  6. //@JsonInclude(JsonInclude.Include.NON_NULL)
  7. @JsonInclude(JsonInclude.Include.ALWAYS)
  8. @Data
  9. @Accessors(chain = true)
  10. public class TestJsonInclude {
  11. private Long id;
  12. private String name;
  13. private Integer age;
  14. private Boolean isAdmin = true;
  15. private Optional<String> optional;
  16. @JsonInclude(JsonInclude.Include.NON_NULL)
  17. private AtomicReference<String> atomic;
  18. @JsonInclude(JsonInclude.Include.NON_EMPTY)
  19. private List<Map<String, Object>> hobby;
  20. @JsonInclude(JsonInclude.Include.NON_NULL)
  21. public AtomicReference<String> getAtomic() {
  22. return atomic;
  23. }
  24. }

自定义序列化类


/**
 * <p>
 * 自定义序列化规则
 * </p>
 *
 * @author xupu
 * @since 2022/5/26 17:19:11
 */
public class CustomFilter {

    @Override
    public boolean equals(Object obj) {
        // null,或者不是字符串就返回true,意味着不被序列化
        if(null == obj || !(obj instanceof String)) {
            return true;
        }

        // 长度大于2就返回true,意味着不被序列化
        return ((String) obj).length() > 2;
    }

}

测试案例类

public class JsonIncludeDemo {

    private static ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
    }

    public static void main(String[] args) throws JsonProcessingException {
        Class<TestJsonInclude> clazz = TestJsonInclude.class;
        if(clazz.isAnnotationPresent(JsonInclude.class)) {
            // 获取 "类" 上的注解
            JsonInclude getAnnotation = clazz.getAnnotation(JsonInclude.class);
            testInclude(String.valueOf(getAnnotation.value()));
            // 默认策略,任何情况都执行序列化
            // 非空
            // null的不会序列化,但如果类型是AtomicReference,依然会被序列化
            // null、集合数组等没有内容、空字符串等,都不会被序列化
            // 如果字段是默认值,就不会被序列化
            // 此时要指定valueFilter属性,该属性对应一个类,用来自定义判断被JsonInclude修饰的字段是否序列化
            // 当JsonInclude在类和属性上都有时,优先使用属性上的注解,此时如果在序列化的get方法上使用了JsonInclude,并设置为USE_DEFAULTS,就会使用类注解的设置
        }
    }

    private static void testInclude(String type) throws JsonProcessingException {
        System.out.println("//@JsonInclude(JsonInclude.Include." + type);
        if("NonAbsent".equals(type)) {
            mapper.registerModule(new Jdk8Module());
        }
        TestJsonInclude test = new TestJsonInclude().setId(1L)
                                                    .setName("")
                                                    .setAge(null)
                                                    .setOptional(Optional.empty())
                                                    .setAtomic(new AtomicReference<>())
                                                    .setHobby(new ArrayList<>());
        System.out.println(mapper.writeValueAsString(test));
    }
}