springboot时间格式化与空值格式化

时间:2025-04-02 14:07:39
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.deser.*; import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import org.apache.commons.lang3.StringUtils; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import java.io.IOException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; class JacksonHttpMessageConverter extends MappingJackson2HttpMessageConverter { /** * 处理数组类型的null值 */ public class NullArrayJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { if (value == null) { jgen.writeStartArray(); jgen.writeEndArray(); } } } /** * 处理字符串类型的null值 */ public class NullStringJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(StringUtils.EMPTY); } } /** * 处理数字类型的null值 */ public class NullNumberJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeNumber(0); } } /** * 处理布尔类型的null值 */ public class NullBooleanJsonSerializer extends JsonSerializer<Object> { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeBoolean(false); } } /** * 处理LocalDateTime类型的值 */ public class LocalDateTimeSerializer extends JsonSerializer<Object> { private final DateTimeFormatter formatter= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:"); @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(formatter.format((LocalDateTime) o)); } } /** * 处理LocalDateTime类型的值 */ public class LocalDateSerializer extends JsonSerializer<Object> { private final DateTimeFormatter formatter= DateTimeFormatter.ofPattern("yyyy年MM月dd日"); @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(formatter.format((LocalDate) o)); } } //序列化修改器 public class MyBeanSerializerModifier extends BeanSerializerModifier { @Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { //循环所有的beanPropertyWriter for (Object beanProperty : beanProperties) { BeanPropertyWriter writer = (BeanPropertyWriter) beanProperty; //判断字段的类型,如果是array,list,set则注册nullSerializer if (isArrayType(writer)) { //给writer注册一个自己的nullSerializer writer.assignNullSerializer(new NullArrayJsonSerializer()); } else if (isNumberType(writer)) { writer.assignNullSerializer(new NullNumberJsonSerializer()); } else if (isBooleanType(writer)) { writer.assignNullSerializer(new NullBooleanJsonSerializer()); } else if (isStringType(writer)) { writer.assignNullSerializer(new NullStringJsonSerializer()); } else if (isDateType(writer)){ writer.assignNullSerializer(new NullStringJsonSerializer()); } else if (isLcDateTimeType(writer)) { //非空使用自定义序列化方式 writer.assignSerializer(new LocalDateTimeSerializer()); writer.assignNullSerializer(new NullStringJsonSerializer()); }else if (isLcDateType(writer)) { //非空使用自定义序列化方式 writer.assignSerializer(new LocalDateSerializer()); writer.assignNullSerializer(new NullStringJsonSerializer()); } } return beanProperties; } /** * 是否是数组 */ private boolean isArrayType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return clazz.isArray() || Collection.class.isAssignableFrom(clazz); } /** * 是否是string */ private boolean isStringType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz); } /** * 是否是int */ private boolean isNumberType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return Number.class.isAssignableFrom(clazz); } /** * 是否是boolean */ private boolean isBooleanType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return clazz.equals(Boolean.class); } /** * 是否是LocalDate */ private boolean isDateType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return clazz.equals(Date.class); } /** * 是否是LocalDate */ private boolean isLcDateType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return clazz.equals(LocalDate.class); } /** * 是否是LocalDateTime */ private boolean isLcDateTimeType(BeanPropertyWriter writer) { Class<?> clazz = writer.getType().getRawClass(); return clazz.equals(LocalDateTime.class); } } //反序列化修改器 public class MyBeanDeSerializerModifier extends BeanDeserializerModifier { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { Class<?> aClass = deserializer.handledType(); if (aClass.equals(LocalDateTime.class)) { deserializer=new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:")); } if (aClass.equals(LocalDate.class)) { deserializer=new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy年MM月dd日")); } return deserializer; } } //自定义转换会让全局LocalDateTime序列化失效,默认转Long型 JacksonHttpMessageConverter() { DeserializerFactory dFactory = BeanDeserializerFactory.instance.withDeserializerModifier(new MyBeanDeSerializerModifier()); ObjectMapper objectMapper = new ObjectMapper(null, null, new DefaultDeserializationContext.Impl(dFactory)); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))//只对Date生效 .setSerializerFactory(getObjectMapper().getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier())); setObjectMapper(objectMapper); } }