org.springframework.validation.BindException异常解决

2024-03-04 19:18

本文主要是介绍org.springframework.validation.BindException异常解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

org.springframework.validation.BindException异常解决

一. 异常现象

我在进行开发平台后台管理项目开发的时候,需要对token进行管理,其中需要对token进行编辑,效果如下:

结果在编辑token的时候,产生了如下现象:

token无法被编辑,阻塞了编辑操作的正常进行!

查看浏览器控制台,发现出现了400状态码:

 并且开发工具控制台出现如下异常信息:

[http-nio-8080-exec-22] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'userToken' on field 'expireTime': rejected value [1588986551000]; codes [typeMismatch.userToken.expireTime,typeMismatch.expireTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userToken.expireTime,expireTime]; arguments []; default message [expireTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'expireTime'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "1588986551000"]
Field error in object 'userToken' on field 'startTime': rejected value [1588468151000]; codes [typeMismatch.userToken.startTime,typeMismatch.startTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userToken.startTime,startTime]; arguments []; default message [startTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startTime'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "1588468151000"]]

二. 异常原因

从上面的异常信息中,我提取出核心的异常信息如下:

 ...Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'expireTime'...nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "1588986551000"​rejected value [1588986551000]; codes [typeMismatch.userToken.expireTime,typeMismatch.expireTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userToken.expireTime,expireTime]; arguments []; default message [expireTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'expireTime'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "1588986551000"]

从异常信息可以看出,是因为前端页面以字符串形式传递日期时间字符串到后台接口,默认的SpringMVC处理器无法将java.lang.String类型的字符串转换成java.util.Date类型,进而导致IllegalArgumentException: Could not parse date,归根结底就是前端页面中的日期时间字符串与后端JavaBean类中的Date类型不匹配,typeMismatch.userToken.expireTime,typeMismatch.java.util.Date,typeMismatch!

三. 解决办法

解决办法其实有多种,其实只要保证前后端参数可以实现转换就行了,所以基于这种思路,我提供如下几种解决办法。

解决方法一:

在后端的日期类型的字段上,添加如下注解:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date expireTime;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date startTime;

因为我的前端页面,是普通的html页面,且参数是以表单形势传递的,如下:

 

所以利用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")注解格式化前端传递进来的日期时间参数形式;

利用@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")格式化后端对外输出的日期时间格式。

解决方法二:

第一种解决方法,需要在每个有日期时间类型字段的类中,都添加那样的2个注解,当代码较多时,就有些麻烦,可以编写一个全局的转换器,代码如下:

package com.yyg.openapi.convert;import org.springframework.core.convert.converter.Converter;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** @Author 一一哥* @Blame yiyige* @Since Created in 2020/6/29*/
public class CustomDateConverter implements Converter<String, Date> {@Overridepublic Date convert(String source) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {return simpleDateFormat.parse(source);} catch (ParseException e) {e.printStackTrace();}return null;}}

 然后把该类在spring.xml文件中进行注册配置。

<!--全局的日期时间转换器,解决前后端时间类型不匹配而导致的400异常!--><bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><bean class="com.yyg.openapi.convert.CustomDateConverter"/></list></property></bean>

此时JavaBean类中的属性,只需要格式化对外输出的类型,如下即可:

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

 

这篇关于org.springframework.validation.BindException异常解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/774182

相关文章

IDEA和GIT关于文件中LF和CRLF问题及解决

《IDEA和GIT关于文件中LF和CRLF问题及解决》文章总结:因IDEA默认使用CRLF换行符导致Shell脚本在Linux运行报错,需在编辑器和Git中统一为LF,通过调整Git的core.aut... 目录问题描述问题思考解决过程总结问题描述项目软件安装shell脚本上git仓库管理,但拉取后,上l

解决docker目录内存不足扩容处理方案

《解决docker目录内存不足扩容处理方案》文章介绍了Docker存储目录迁移方法:因系统盘空间不足,需将Docker数据迁移到更大磁盘(如/home/docker),通过修改daemon.json配... 目录1、查看服务器所有磁盘的使用情况2、查看docker镜像和容器存储目录的空间大小3、停止dock

idea npm install很慢问题及解决(nodejs)

《ideanpminstall很慢问题及解决(nodejs)》npm安装速度慢可通过配置国内镜像源(如淘宝)、清理缓存及切换工具解决,建议设置全局镜像(npmconfigsetregistryht... 目录idea npm install很慢(nodejs)配置国内镜像源清理缓存总结idea npm in

idea突然报错Malformed \uxxxx encoding问题及解决

《idea突然报错Malformeduxxxxencoding问题及解决》Maven项目在切换Git分支时报错,提示project元素为描述符根元素,解决方法:删除Maven仓库中的resolv... 目www.chinasem.cn录问题解决方式总结问题idea 上的 maven China编程项目突然报错,是

在Ubuntu上打不开GitHub的完整解决方法

《在Ubuntu上打不开GitHub的完整解决方法》当你满心欢喜打开Ubuntu准备推送代码时,突然发现终端里的gitpush卡成狗,浏览器里的GitHub页面直接变成Whoathere!警告页面... 目录一、那些年我们遇到的"红色惊叹号"二、三大症状快速诊断症状1:浏览器直接无法访问症状2:终端操作异常

mybatis直接执行完整sql及踩坑解决

《mybatis直接执行完整sql及踩坑解决》MyBatis可通过select标签执行动态SQL,DQL用ListLinkedHashMap接收结果,DML用int处理,注意防御SQL注入,优先使用#... 目录myBATiFBNZQs直接执行完整sql及踩坑select语句采用count、insert、u

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT

Java JDK Validation 注解解析与使用方法验证

《JavaJDKValidation注解解析与使用方法验证》JakartaValidation提供了一种声明式、标准化的方式来验证Java对象,与框架无关,可以方便地集成到各种Java应用中,... 目录核心概念1. 主要注解基本约束注解其他常用注解2. 核心接口使用方法1. 基本使用添加依赖 (Maven

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

Python异常处理之避免try-except滥用的3个核心原则

《Python异常处理之避免try-except滥用的3个核心原则》在Python开发中,异常处理是保证程序健壮性的关键机制,本文结合真实案例与Python核心机制,提炼出避免异常滥用的三大原则,有需... 目录一、精准打击:只捕获可预见的异常类型1.1 通用异常捕获的陷阱1.2 精准捕获的实践方案1.3