smaallseventeen 2022-04-26 16:13 采纳率: 66.7%
浏览 213
已结题

String转LocalDateTime没有效果

前端日期选择器传了"releaseTime":"2022-4-26"给后端,然后出现这个错误

2022-04-26 16:04:31.897  WARN 8364 --- [nio-7070-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2022-4-26": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-4-26' could not be parsed at index 5; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2022-4-26": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-4-26' could not be parsed at index 5<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 4, column: 19] (through reference chain: com.example.wy.entity.Notice["releaseTime"])]
{
    "timestamp": "2022-04-26T08:04:31.905+00:00",
    "status": 400,
    "error": "Bad Request",
    "path": "/notice"
}

之后查找方案加过注解@JsonFormat(pattern = "yyyy-MM-dd")但是还是没有用,有什么办法解决嘛
  • 写回答

5条回答 默认 最新

  • 不甘_ 2022-04-26 16:33
    关注

    个人喜好将下面两种注解都加上,一个是前端转后端,一个是后端转前端的。

    
      /**
         * 创建时间
         */
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        //2022-03-22T18:32
        @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
        @Column(name = "create_time")
        private Date createTime;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    
    "timestamp": "2022-04-26T08:04:31.905+00:00"
    
    

    这一段中有个字符T,不知道会不会是这个的问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 5月5日
  • 已采纳回答 4月27日
  • 创建了问题 4月26日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line