解决黑马点评项目中实现商铺缓存与数据库的双写一致问题出现的问题,LocalDateTime在项目中的使用(LocalDateTime与前端的时间戳互转)

文章讲述了在Java项目中遇到的LocalDateTime类型与前端时间戳转换的问题,以及SpringBoot默认使用Jackson导致的问题。为了解决这个问题,作者提供了自定义的LocalDateTimeSerializer和LocalDateTimeDeserializer来处理转换,并配置了SpringBoot使用FastJson进行HTTP消息转换,确保前后端数据的一致性。
摘要由CSDN通过智能技术生成

解决黑马点评项目中实现商铺缓存与数据库的双写一致问题出现的问题:时间问题
2023-01-10 10:41:48.177 ERROR 1796 — [nio-8081-exec-2] com.hmdp.config.WebExceptionAdvice : org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: raw timestamp (1642069999999) not allowed for java.time.LocalDateTime: need additional information such as an offset or time-zone (see class Javadocs); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: raw timestamp (1642069999999) not allowed for java.time.LocalDateTime: need additional information such as an offset or time-zone (see class Javadocs)
at [Source: (PushbackInputStream); line: 9, column: 17] (through reference chain: com.hmdp.entity.Shop[“updateTime”])

Java的LocalDateTime 转为 前端的时间戳


/**
 * 由于 LocalDateTime 类型在转换 JSON 的时候,并不能被转换为字符串,使用 @JsonFormat 只能转换为指定的 pattern 类型,因此我们需要自定义一个序列化执行器
 * LocalDateTime 序列化(将 LocalDateTime类型 转换为 时间戳 返回给前端 )
 *
 */
public class LocalDateTimeSerializer implements ObjectSerializer {
   
    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
   
        if (object != null) {
   
            LocalDateTime localDateTime = (LocalDateTime) object;
            //将localDateTime转换为中国区(+8)时间戳。
            serializer.write(localDateTime.toInstant(ZoneOffset.ofHours(8)).to
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值