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

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日