• 【精品】Springboot 接收发送日期类型的数据


    问题

    在这里插入图片描述

    无法请求到后台,后台报错:[Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property

    在这里插入图片描述

    2023-10-02T09:26:16.069+08:00  WARN 14296 --- [p-nio-80-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public com.tiku.common.bean.ResultBean com.tiku.controller.manger.BookController.list(com.tiku.domain.query.BookQuery) with 2 errors: [Field error in object 'bookQuery' on field 'beginTime': rejected value [2000-12-12 12:12:12]; codes [typeMismatch.bookQuery.beginTime,typeMismatch.beginTime,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [bookQuery.beginTime,beginTime]; arguments []; default message [beginTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'beginTime'; Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value [2000-12-12 12:12:12]]] [Field error in object 'bookQuery' on field 'endTime': rejected value [2020-12-12 12:12:12]; codes [typeMismatch.bookQuery.endTime,typeMismatch.endTime,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [bookQuery.endTime,endTime]; arguments []; default message [endTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'endTime'; Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value [2020-12-12 12:12:12]]] ]
    
    • 1

    方式一

    在application.yml中添加:

    spring:
      mvc:
        format:
          date-time: yyyy-MM-dd HH:mm:ss
      #时间格式:返回格式化日期
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    方式二

    在类的属性上添加注解:

    // 接收时间类型
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    //返回时间类型
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private LocalDateTime beginTime;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    说明:

    • @DateTimeFormat :该注解自动会解析处理,会把字符串类型 按照格式yyyy-MM-dd HH:mm:ss 转换成时间类型
    • @JsonFormat:该注解是从数据库查询出来,返回到页面的的转换. 把时间类型 转换成JSON格式类型,前提取出进行展示.

    小结

    实际项目开发中,一般采用第一种方式全局设置日期处理方式,采用第二种方式针对特定情况进行处理。

  • 相关阅读:
    罗丹明标记的葡聚糖 70k,RB-Dextran,MW:70K
    在WIN7下特定图片SystemParametersInfo失败,但是GetLastError()返回0
    P1941 [NOIP2014 提高组] 飞扬的小鸟
    tenacity发生异常/失败/错误时重试retry机制,Python
    流形上的预积分(中)
    毕业设计之基于知识图谱的电影推荐问答系统(python完整源码+说明文档+演示视频)
    模糊查询like用法实例(Bee)
    获取1688店铺详情 API接口(获取卖家昵称、店铺类型、公司信息、店铺标题、店铺主页)
    【Django框架】——22 Django视图 04 HttpRequest对象
    pandas read_json时ValueError: Expected object or value的解决方案
  • 原文地址:https://blog.csdn.net/lianghecai52171314/article/details/133484413