Jackson自定义值为null的字段默认值序列化

自定义 JsonSerializer

package com.sdhs.wash.admin.component.jackson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

/**
 * JackJson空值处理
 * 只对bean处理
 * 

* 字符串 "" * 集合 [] * map {} * * @author seer * @version 1.0 * @date 2021/8/24 14:38 */ public class NullValueSerializer extends JsonSerializer { @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { String fieldName = gen.getOutputContext().getCurrentName(); Class clazz = gen.getCurrentValue().getClass(); if (Map.class.isAssignableFrom(clazz)) { // map的不处理 gen.writeNull(); return; } Class fieldType; try { fieldType = gen.getCurrentValue().getClass().getDeclaredField(fieldName).getType(); } catch (NoSuchFieldException e) { throw new IOException(e); } // 字符串 "" if (String.class.isAssignableFrom(fieldType)) { gen.writeString(""); return; } // 集合 [] if (Collection.class.isAssignableFrom(fieldType)) { gen.writeStartArray(); gen.writeEndArray(); return; } // 布尔 false // if (Boolean.class.isAssignableFrom(fieldType)) { // gen.writeBoolean(false); // return; // } // map {} if (Map.class.isAssignableFrom(fieldType)) { gen.writeStartObject(); gen.writeEndObject(); return; } // 其它还是null gen.writeNull(); } }

注册到 ObjectMapper

public class JsonMapper {

    private static ObjectMapper mapper;

    public static ObjectMapper getMapper() {
        if (null == mapper) {
            mapper = new ObjectMapper();
            // 反序列化,未知字段不失败
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            // 序列化,空bean不失败,被 @JsonIgnore 注解的bean
            mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            mapper.registerModule(new Jdk8Module());
            // 默认 LocalDateTime 格式
            JavaTimeModule timeModule = new JavaTimeModule();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DatePattern.yyyy_MM_dd_HH_mm_ss, Locale.CHINA);
            timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
            timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
            mapper.registerModule(timeModule);
            // 默认 Date 格式
            mapper.setDateFormat(new SimpleDateFormat(DatePattern.yyyy_MM_dd_HH_mm_ss));
            // 注解 Introspector 不处理会导致注解失效
            mapper.setAnnotationIntrospector(new JsonFormatIntrospector());
            // 所有序列化的对象都将按改规则进行系列化
            mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
            // null 序列化
            mapper.getSerializerProvider().setNullValueSerializer(new NullValueSerializer());
        }
        return mapper;
    }
}

使用

JsonMapper.getMapper().writeValueAsString(xxx);

你可能感兴趣的:(Jackson自定义值为null的字段默认值序列化)