springboot 全局时间转换器
目录结构
- 前言
- 前端操作
- Network Headers
- Network preview
- 后台日志信息
- 问题所在
- 解决办法一
- 解决办法二
- 参考链接
前言
- 前端框架:layui mini
- 后台框架:springboot
layui table按钮监听提交数据,进行数据的操作,后台报错,解决过程记录;
前端操作
Network Headers
Network preview
后台日志信息
全部报错信息
Field error in object 'TAccountAgency' on field 'createTime': rejected value [2022-08-17 15:46:18]; codes [typeMismatch.TAccountAgency.createTime,typeMismatch.createTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [TAccountAgency.createTime,createTime]; arguments []; default message [createTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.util.Date] for value '2022-08-17 15:46:18'; nested exception is java.lang.IllegalArgumentException]]
主要报错信息
Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTime';
问题所在
从日志信息可以看出,是因为前端页面以字符串形式传递日期时间字符串到后台接口,默认的springboot时间处理器无法将java.lang.String类型的字符串转换成java.util.Date类型,进而导致“Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘createTime’;”,最后总结就是前端页面中的日期时间字符串与后端JavaBean类中的Date类型不匹配,
解决办法一
- @JsonFormat(pattern=“yyyy-MM-dd HH:mm:ss”, timezone=“GMT+8”)
- 格式化前端传递进来的日期时间参数形式
- @DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)
- 格式化后端对外输出的日期时间格式
在后端的日期类型的字段上添加以上注解代码片
- 格式化后端对外输出的日期时间格式
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
解决办法二
解决办法一,需要在每个有时间类型字段上都添加注解,当代码较多时,就有些麻烦,可以编写一个全局的转换器,代码如下 代码块
:
实现org.springframework.core.convert.converter.Convert接口,来完成日期格式的转换
package com.zaxk.config;import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;
import java.util.Date;/*** @description: 全局时间转换器* @author: Mr.Jkx* @time: 2022/8/17 16:53*/
@Component
public class CourseDateConverter implements Converter<String, Date> {private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";private static final String dateFormata = "yyyy-MM-dd HH:mm:ss";private static final String shortDateFormat = "yyyy-MM-dd";private static final String shortDateFormata = "yyyy/MM/dd";private static final String timeStampFormat = "^\\d+$";@Overridepublic Date convert(String value) {if (StringUtils.isEmpty(value)) {return null;}value = value.trim();try {if (value.contains("-")) {SimpleDateFormat formatter;if (value.contains(":")) {// yyyy-MM-dd HH:mm:ss 格式formatter = new SimpleDateFormat(dateFormat);} else {// yyyy-MM-dd 格式formatter = new SimpleDateFormat(shortDateFormat);}return formatter.parse(value);} else if (value.matches(timeStampFormat)) {//时间戳Long lDate = new Long(value);return new Date(lDate);} else if (value.contains("/")) {SimpleDateFormat formatter;if (value.contains(":")) {// yyyy/MM/dd HH:mm:ss 格式formatter = new SimpleDateFormat(dateFormata);} else {// yyyy/MM/dd 格式formatter = new SimpleDateFormat(shortDateFormata);}return formatter.parse(value);}} catch (Exception e) {throw new RuntimeException(String.format("parser %s to Date fail", value));}throw new RuntimeException(String.format("parser %s to Date fail", value));}
}
此时JavaBean类中的属性,只需要格式化对外输出的类型,如下即可 代码块
:
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
private Date createTime;@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
private Date updateTime;
参考链接
- pringBoot 通过Converter转化 date类型参数
- SpringBoot 通过Converter转化 date类型参数
- springboot~对@RequestParam中Date参数的适配
springboot 全局时间转换器相关推荐
- java date 格式化_3种 Springboot 全局时间格式化方式,别再写重复代码了
原文:3种 Springboot 全局时间格式化方式,别再写重复代码了 掘金 作者: 程序员内点事 时间格式化在项目中使用频率是非常高的,当我们的API接口返回结果,需要对其中某一个date字段属性进 ...
- 3种 Springboot 全局时间格式化方式,别再写重复代码了
时间格式化在项目中使用频率是非常高的,当我们的 API 接口返回结果,需要对其中某一个 date 字段属性进行特殊的格式化处理,通常会用到 SimpleDateFormat 工具处理. SimpleD ...
- Springboot实战:3种 Springboot 全局时间格式化方式
时间格式化在项目中使用频率是非常高的,当我们的 API 接口返回结果,需要对其中某一个 date 字段属性进行特殊的格式化处理,通常会用到 SimpleDateFormat 工具处理. SimpleD ...
- SpringBoot日期时间全局出入参格式化-3:全局Timestamp出入参
SpringBoot时间出入参格式化-3:全局Timestamp出入参 上一篇中使用的是全局使用字符串处理时间参数.本文提供第三种处理方式:使用全局时间戳方式处理入参时间,如入参:1657096088 ...
- springboot 2.0 配置全局时间格式化
springboot 2.0 配置全局时间格式化 方式一: 在yml配置文件中添加以下配置 spring:jackson:date-format: yyyy-MM-dd HH:mm:sstime-zo ...
- 转载:Springboot全局事务处理
Springboot全局事务处理 本文完全转载:原文为做一只快乐的猴子!的文章:Springboot全局事务处理 只是作为备忘和学习,如有侵权,会删除,谢谢 什么是全局事务 Spring Boot(S ...
- SpringBoot全局异常处理及前端请求参数校验
SpringBoot全局异常捕获处理及参数校验 文章目录 SpringBoot全局异常捕获处理及参数校验 为什么要用全局异常处理? 如何进行全局异常捕获和处理? 统一结果封装 统一返回结果 枚举类 使 ...
- 奇淫巧技,springboot 全局日期格式化处理,有点香!
最近面了一些公司,有一些 Java方面的架构.面试资料,有需要的小伙伴可以在公众号[程序员内点事]里,无套路自行领取 说在前边 最近部门几位同事受了一些委屈相继离职,共事三年临别之际颇有不舍,待一切手 ...
- springBoot 全局异常方式处理自定义异常 @RestControllerAdvice + @ExceptionHandler
springBoot 全局异常方式处理自定义异常 @RestControllerAdvice + @ExceptionHandler 参考文章: (1)springBoot 全局异常方式处理自定义异常 ...
最新文章
- jquery动态加载问题
- python高通滤波器设计_python实现直方图均衡化,理想高通滤波与高斯低通滤波
- IDEA——常用基础设置
- esxi备份,datastore,vmdk
- php图片上传不现实路径指向错误,上传图片提示这个错误怎么办?
- 分块-洛谷P3203 [HNOI2010]BOUNCE 弹飞绵羊
- 大众考虑投资中国汽车零部件供应商 潜在目标包括国轩高科
- 智能音箱扎堆的技术红海,Rokid 如何杀出一条血路?
- python求三个整数最大值_python 练习题:定义一个getMax()函数,返回三个数(从键盘输入的整数)中的最大值。...
- SpringBoot +自定义dao框架 自定义注解管理多数据源与事务
- 有哪些好用的在线条形码生成器?
- 根据身份证号码获取年龄
- **alon_MM DMA Interface for PCIe使用详解
- 巧妙按键法,包含几乎所有可以实现的按键功能(单击,双击,N击,长按等功能)
- 什么是三网合一短信接口呢
- 政策 | 辅导班的“超纲教学”凉了?教育部印发六科负面清单!
- K 凸函数的一些性质和相关证明
- ftp命令行登陆 用法指南
- 1.11CSS的基本语法
- Latex写文章时插入单栏图片和双栏图片方法