关于LocalDateTime的全局返回时间带“T“的时间格式处理

4 篇文章 0 订阅
3 篇文章 0 订阅


  关于背景的大概描述,如下。
  时间类型LocalDateTime相较于于Date的确有自己独特的优势,因此在之后的版本迭代上,将部分隔离开的模块使用了新型的LocalDateTime替代了Date,因已经使用了全局时间转换,但是测试的时候发现返回的时间格式很奇怪 2022-01-01T00:00:00.000,全局时间序列化、反序列化并没有起作用,寻找相关资料和DEGBU之后将相关资料整理如下。
目前常用的时间类型格式如下:

时间类型时间默认格式
DateTue Feb 01 00:00:00 CST 2022
Timestamp2022-01-01 00:00:00.0
LocalDateTime2022-01-01T00:00:00.000

1.关于jackson的全局返回带T的处理方式

对于Date和Timestamp在引入LocalDateTime之前是经过spring.jackson.date-format进行全局数据绑定格式化时间类型。

spring:
	jackson:
		date-format: 格式化类

在格式化类中添加对LocalDateTime的格式化语句发现无效果。经过搜索资料后发现在使用jackson在对localDateTime类型的序列化和反序列化方法另有其他方法。(这里不输出关于源码内的信息。可自行查看。)

// 涉及源码类 其余的可自行debug追踪
Jackson2ObjectMapperBuilder
Jackson2ObjectMapperBuilderCustomizer // 最终做处理的服务类
JacksonAutoConfiguration
@Configuration
public class LocalDateTimeSerializerConfig {
	// jackson注册的时候会将序列化和反序列化的方法提前注册进,做数据转换会自行根据具体的实现去调用。
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer());
        };
    }

    /**
     * 反序列化
     */
    public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
        @Override
        public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext)
                throws IOException {
            long timestamp = p.getValueAsLong();
            if (timestamp > 0) {
                return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
            } else {
                return null;
            }
        }
    }

}

2.关于fastjson的全局返回带T的处理方式

使用fastjson进行时间格式化处理,自己写的类去实现 WebMvcConfigurer配置类,重写父类方法configureMessageConverters,在进行全局时间返回格式绑定后,之前使用的格式化时间类是fastjson自身提供的SimpleDateFormatSerializer方法,发现在进行对LocalDateTime添加后进行时间转化返回后存在异常,异常信息提示LocalDateTime不能强转为Date,进行多次DEBUG跟踪后定位到了SimpleDateFormatSerializer方法内部,会将拿到的value直接进行Date的强转,SimpleDateFormatSerializer源码如下

public class SimpleDateFormatSerializer implements ObjectSerializer {

    private final String pattern;

    public SimpleDateFormatSerializer(String pattern){
        this.pattern = pattern;
    }

    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    	if (object == null) {
    		serializer.out.writeNull();
    		return;
    	}
    	// 对LocalDateTime 进行强制转换,此处会报错。因此,主要的重点改造就是此处。
        Date date = (Date) object;
        SimpleDateFormat format = new SimpleDateFormat(pattern, serializer.locale);
        format.setTimeZone(serializer.timeZone);
        String text = format.format(date);
        serializer.write(text);
    }
}

既然已经发现问题的关键点那对关键点进行处理即可,目前有两个方法:

  1. 写好SimpleDateFormatSerializer的继承类
  2. 实现ObjectSerializer的write方法进行处理。

我选用的是第一种方式对SimpleDateFormatSerializer集成后重写write方法,因为之前的时间格式已经使用的SimpleDateFormatSerializer进行处理,为了稳定进行最小程度的调整。结果如下

public class OwnSimpleDateFormatSerializer extends SimpleDateFormatSerializer {

    private final String pattern;

    public OwnSimpleDateFormatSerializer (String pattern) {
        super(pattern);
        this.pattern = pattern;
    }


    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        if (object == null) {
            serializer.out.writeNull();
        } else {
            Date date = null;
            // 改造内容 增加类型判断进行处理
            if (object instanceof LocalDateTime) {
                LocalDateTime localDateTime = (LocalDateTime) object;
                date = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
            } else {
                date = (Date)object;
            }
            SimpleDateFormat format = new SimpleDateFormat(this.pattern, Locale.SIMPLIFIED_CHINESE);
            TimeZone timeZone = new ZoneInfo();
            timeZone.setRawOffset(28800000);
            timeZone.setID("Asia/Shanghai");
            format.setTimeZone(timeZone);
            String text = format.format(date);
            serializer.write(text);
        }
    }
}
// 涉及类
WebMvcConfigurer
SimpleDateFormatSerializer // 之前使用的时间格式化类
ObjectSerializer // 最终执行时间格式化处理的服务类
FastJsonConfig // 配置类
SerializeConfig // 序列化配置信息
@Component
public class DefaultMvcFilter implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		 // ...
		 // 重点在此 对FastJsonConfig进行配置
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		 // 修改全局json配置
		SerializeConfig serializeConfig = SerializeConfig.globalInstance;
		 // 格式化时间
		serializeConfig.put(Date.class, new OwnSimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
		serializeConfig.put(java.sql.Date.class, new OwnSimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
		serializeConfig.put(java.sql.Timestamp.class, new OwnSimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
		serializeConfig.put(java.sql.Time.class, new OwnSimpleDateFormatSerializer("HH:mm:ss"));
		// 对LocalDateTime进行时间格式化 注意序列化的实现类 是需要自己写的,并不是使用fastjson自带的SimpleDateFormatSerializer
		serializeConfig.put(LocalDateTime.class, new OwnSimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
		fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
		fastJsonConfig.setSerializeConfig(serializeConfig);
		fastJsonConfig.setSerializerFeatures(
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.IgnoreErrorGetter,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.WriteNullBooleanAsFalse,
				SerializerFeature.DisableCircularReferenceDetect
		);
    }
}

以上对于jackson和fastjson改造完成后进行测试,发现时间格式全部展示正确,以此来记录问题发生和处理方式。主要还是根据源码跟踪来确定问题,问题确定后的处理就会有很多方式了,选择最适用的处理方式即可。

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot应用程序中使用MyBatis时,可以使用TypeHandler来解决LocalDateTime类型的全局配置时间转换问题。 首先,创建一个实现了TypeHandler接口的类,例如:LocalDateTimeTypeHandler。 ``` public class LocalDateTimeTypeHandler implements TypeHandler<LocalDateTime> { @Override public void setParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException { if (parameter != null) { ps.setTimestamp(i, Timestamp.valueOf(parameter)); } else { ps.setNull(i, Types.TIMESTAMP); } } @Override public LocalDateTime getResult(ResultSet rs, String columnName) throws SQLException { Timestamp timestamp = rs.getTimestamp(columnName); return getLocalDateTime(timestamp); } @Override public LocalDateTime getResult(ResultSet rs, int columnIndex) throws SQLException { Timestamp timestamp = rs.getTimestamp(columnIndex); return getLocalDateTime(timestamp); } @Override public LocalDateTime getResult(CallableStatement cs, int columnIndex) throws SQLException { Timestamp timestamp = cs.getTimestamp(columnIndex); return getLocalDateTime(timestamp); } private LocalDateTime getLocalDateTime(Timestamp timestamp) { if (timestamp != null) { return timestamp.toLocalDateTime(); } return null; } } ``` 然后,在MyBatis的配置文件中将该TypeHandler注册为全局TypeHandler。 ``` <configuration> <typeHandlers> <typeHandler handler="com.example.LocalDateTimeTypeHandler"/> </typeHandlers> </configuration> ``` 这样,在使用LocalDateTime类型的时候,MyBatis就会自动调用该TypeHandler进行转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值