admin管理员组文章数量:1538079
目录结构
- 前言
- 前端操作
- 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+$";
@Override
public 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
版权声明:本文标题:springboot 全局时间转换器 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1726965967a1092218.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论