zoukankan      html  css  js  c++  java
  • spring boot/cloud开发遇到的坑(持续)

    目录

    K.0000 入参为Date类型

    K.0000 入参为Date类型

    入参为Date类型时,默认情况下发生了错误:

    2021-09-01 14:43:19.855  WARN 22940 --- [io-30005-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : 
    Resolved [org.springframework.http.converter.HttpMessageNotReadableException: 
    JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2021-09-01 14:21:07": 
    not a valid representation (error: Failed to parse Date value '2021-09-01 14:21:07': 
    Cannot parse date "2021-09-01 14:21:07": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', 
    parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: 
    Cannot deserialize value of type `java.util.Date` from String "2021-09-01 14:21:07": not a valid representation 
    (error: Failed to parse Date value '2021-09-01 14:21:07': Cannot parse date "2021-09-01 14:21:07": 
    while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))

    S.B.版本:2.5.3

    入参DTO中定义:

    /**
    	 * 发布时间
    	 */
    	private Date postTime;

    传入参数为:

    “2021-09-01 14:21:07”

    点击提交,发生了开始的错误。

    解决方案1:单个参数

    修改源码:增加 @DateTimeFormat、@JsonFormat 即可,其实,只需要添加 @JsonFormat 即可。

    	/**
    	 * 发布时间
    	 */
    	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    	private Date postTime;

    上面的方式,解决了 入参为Date类型转换的问题。

    对于 返回值VO,也需要加上上面的注解才可以确保返回的时间格式:

    返回值VO:
    1、未添加,默认格式
    "postTime": "2021-09-01T14:21:07.000+00:00",
    2、添加后的格式
    "postTime": "2021-09-01 10:29:30",

    @JsonFormat 说明:来自 jackson-annotations-x.x.x.jar 包

    解决方案(2):全局
    添加配置:

    spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

    即可,入参、返回值,都搞定,都是上面的格式,整个项目有效。

    两种方案的优先级:解决方案1更高

    使用解决方案2后,再如下配置入参属性:

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

    此时,传入之前格式的日期出现了异常,并提示:

    JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2021-09-02 11:41:10":
    expected format "yyyy-MM-dd HH:mm:ss.SSS"; nested exception is ...

    此时,入参改为 "postTime":"2021-09-02 11:41:10.123" 即可执行。

    返回值也同样如此。

    当然,还有其它解决方案,比如,定制参数、返回值转换器。

    参考文档:

    1、springboot 日期参数前后台转换问题

    https://blog.csdn.net/springdata_/article/details/82629666

    2、springboot Date类型入参转换问题

    https://blog.csdn.net/authiur/article/details/104963269

    ...然后就对问题进行了一次深入挖掘...

    3、java 日期格式化-- SimpleDateFormat 的使用。字符串转日期,日期转字符串

    https://www.cnblogs.com/daxiong225/p/9547078.html

  • 相关阅读:
    dotnet core 获取 MacAddress 地址方法
    dotnet core 获取 MacAddress 地址方法
    dotnet core 发布只带必要的依赖文件
    dotnet core 发布只带必要的依赖文件
    Developing Universal Windows Apps 开发UWA应用 问答
    Developing Universal Windows Apps 开发UWA应用 问答
    cmd 如何跨驱动器移动文件夹
    cmd 如何跨驱动器移动文件夹
    C++ 驱动开发 error LNK2019
    C++ 驱动开发 error LNK2019
  • 原文地址:https://www.cnblogs.com/luo630/p/15218732.html
Copyright © 2011-2022 走看看