前言
通过实现Jackson2ObjectMapperBuilderCustomizer接口并注册到容器,进行个性化定制,Spring Boot不会覆盖默认ObjectMapper的配置,而是进行了合并增强,具体还会根据Jackson2ObjectMapperBuilderCustomizer实现类的Order优先级进行排序,因此上面的JacksonConfig配置类还实现了Ordered接口。
默认的Jackson2ObjectMapperBuilderCustomizerConfiguration优先级是0,因此如果我们想要覆盖配置,设置优先级大于0即可。
**注意:**在SpringBoot2环境下,不要将自定义的ObjectMapper对象注入容器,这样会将原有的ObjectMapper配置覆盖!
优雅个性化定制Jackson实现示例
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.moyu.common.Constants;
import org.moyu.common.serializer.JacksonLocalDateTimeDeserializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.io.IOException;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* 自定义Jackson配置
*/
@Configuration
public class JacksonConfig implements Jackson2ObjectMapperBuilderCustomizer, Ordered {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
// 将入参中的空字符串""转为null
builder.deserializerByType(String.class, new StdScalarDeserializer<String>(String.class) {
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext ctx)
throws IOException {
// 重点在这儿:如果为空白则返回null
String value = jsonParser.getValueAsString();
if (value == null || "".equals(value.trim())) {
return null;
}
return value;
}
});
// 设置java.util.Date时间类的序列化以及反序列化的格式
builder.simpleDateFormat(Constants.YYYY_MM_DD_HH_MM_SS);
// JSR 310日期时间处理
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(Constants.YYYY_MM_DD_HH_MM_SS)));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(Constants.YYYY_MM_DD)));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(Constants.HH_MM_SS)));
javaTimeModule.addDeserializer(LocalDateTime.class, new JacksonLocalDateTimeDeserializer());
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(Constants.YYYY_MM_DD)));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(Constants.HH_MM_SS)));
builder.modules(javaTimeModule);
// 全局转化Long类型为String,解决序列化后传入前端Long类型精度丢失问题
builder.serializerByType(BigInteger.class, ToStringSerializer.instance);
builder.serializerByType(Long.class, ToStringSerializer.instance);
}
@Override
public int getOrder() {
return 1;
}
}
JacksonLocalDateTimeDeserializer
public class JacksonLocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {
public JacksonLocalDateTimeDeserializer() {
super(LocalDateTime.class);
}
protected JacksonLocalDateTimeDeserializer(Class<?> vc) {
super(vc);
}
public LocalDateTime deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException {
String dateText = jp.getValueAsString();
DateTime parse = DateUtil.parse(dateText);
Instant instant = parse.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalDateTime();
}
}