zoukankan      html  css  js  c++  java
  • @DateTimeFormat、@JsonFormat、@JSONField

    今天在处理时间时遇到

    org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2020-04-08": expected format "yyyy-MM-dd HH:mm:ss"; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-04-08": expected format "yyyy-MM-dd HH:mm:ss"

    属性

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startDate;

    前端传过来的,只有年月日,需要的格式为yyyy-MM-dd HH:mm:ss

    解决的方法

    将前端的日期格式改为

    yyyy-MM-dd  HH:mm:ss

    或者修改属性的注解里pattern的值

    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date startDate;

    补充:

    1.@DateTimeFormat

    spring自带的,将String转换成Date,一般前台给后台传值时用

    @DateTimeFormat(pattern=”yyyy-MM-dd”)

    2.@JsonFormat

    来源于jackson,将Java对象转换成json对象和xml文档,或将json、xml转换成Java对象

    主要用于后台传值到前台

    在属性值上

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")

    3.@JSONField

    来源于fastjson,主要进行JSON解析和序列化

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
  • 相关阅读:
    蛇形填数
    开灯问题
    水仙花数
    C++Primer笔记-----day02
    C++Primer笔记-----day01
    面试智力题
    maven 打包197
    子系统 安装vsftpd197
    office 安装破解197
    oracle 创建多个数据库197
  • 原文地址:https://www.cnblogs.com/baby123/p/12762717.html
Copyright © 2011-2022 走看看