SpringBoot请求日期参数异常(Failed-to-convert-value-of-type-'java-lang-String'-
问题
Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-12-12 12:12'; nested exception is java.lang.IllegalArgumentException
分析
SpringMvc没有设置日期转换配置,直接把当成String类型强转Date类型!
解决方案
第一种方法:
@DateTimeFormat 日期参数格式化注解
@RequestMapping(value = "test")
@ResponseBody
public Map<String,Object> test(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date, @NumberFormat(pattern = "#,###.##")Double number){
Map<String,Object> resultMap = new HashMap<>();
resultMap.put("data",date);
resultMap.put("number",number);
return resultMap;
}
第二种方法:
application.yml设置日期参数格式化配置:
spring:
mvc:
date-format: yyyy-MM-dd HH:mm:ss
返回格式化日期
第一种方法:
在实体类中Date属性上标记:@JsonFormat
pattern:是你需要转换的时间日期的格式
timezone:是时间设置为东八区,避免时间在转换中有误差
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
private Date updateDate;
第二种方法:
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
@NumberFormat 注解 支持数字类型参数格式化格式
SpringBoot请求日期参数异常(Failed-to-convert-value-of-type-'java-lang-String'-的更多相关文章
- SpringBoot 项目启动 Failed to convert value of type 'java.lang.String' to required type 'cn.com.goldenwater.dcproj.dao.TacPageOfficePblmListDao';
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tac ...
- Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
springboot jdbc查询使用LocalDate报:Failed to convert value of type 'java.lang.String' to required type 'j ...
- 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '
没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...
- 完美解决报错Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'
Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' 首先这个错误的意思是 前台页面 ...
- Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found
今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...
- Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFa ...
- Java EE之Struts2异常[No mapping found for dependency [type=java.lang.String, name='actionPackages'#java.lang.RuntimeException]【摘抄】
本博文摘自:http://www.blogjava.net/nkjava/archive/2009/03/29/262705.html 出现这个问题,可能是添加了struts2-codebehind包 ...
- Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-02-06'; nested exception is java.lang.IllegalArgumentException]解决
今天做springbook项目前端输入日期传到数据库保存报了一下错误 Whitelabel Error Page This application has no explicit mapping fo ...
- spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'
在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错: Failed to convert property value of type [java.lang.String] ...
随机推荐
- wumei-smart智能家居开原项目
一.项目简介 物美智能(wumei-smart)]是一套开源的软硬件系统,可用于二次开发和学习,快速搭建自己的智能家居系统. 硬件工程师可以把自己的设备集成到系统:软件工程师可以使用项目中的设备熟悉软 ...
- Luogu P2051「AHOI2009」中国象棋
看见第一眼觉得是状压 \(\text{DP}\)?观察数据范围发现不可做 那按照最常规思路设状态试试? 设状态为\(dp[i][j]\)表示\(i*j\)的棋盘的方案数 好像转移不了欸 要不再来一维? ...
- PXE高效批量网络装机
PXE高效批量网络装机 一.PXE概述 1)PXE (Preboot eXcution Environment) ...
- kali之Metasploit入门
前言 不得不说Metasploit 目前是世界领先的黑客框架.它在某种程度上被几乎每个黑客/渗透者使用.因此,如果您想进入网络安全/渗透测试行业并有所成就,您就必须要需要熟悉它. Metasploit ...
- 接入 SDK 结果翻车了?了解 SDK 的那些事
前段时间,二狗子的朋友圈被工信部发布的<关于下架侵害用户权益 App 名单的通报>给刷屏了.公告中指出有 90 款 App 未按照要求完成整改将会下架.而这 90 款 App 涉及全国各地 ...
- Hive——join的使用
Hive--join的使用 hive中常用的join有:inner join.left join .right join .full join.left semi join.cross join.mu ...
- springMVC-12-整合spring和springmvc
问题1: 好像我们只需要使用springmvc的配置文件作为IOC容器就可以了 --> 需要进行Spring 整合SpringMVC吗? ----> 还是否需要再加入Spring的IOC容 ...
- 两万字Vue.js基础学习笔记(二)
Vue.js学习笔记(二) 4.模块化开发 ES6模块化的导入和导出 我们使用export指令导出了模块对外提供的接口,下面我们就可以通过import命令来加载对应的这个模块了 首先,我们需要在HTM ...
- Linux CentOS 7 在Apache服务器上安装SSL证书
在开发微信小程序的时候,wx.request请求的地址必须是https的,所以只能重新配置服务器. 域名和服务器都是在阿里云上买的,系统是CentOS7,安装了Apache服务器.网上也找了一下,很多 ...
- python算法学习--待续
几个算法网站 算法可视化网站:https://visualgo.net/en,通过动画展示算法实现过程 程序可视化网站:http://www.pythontutor.com/visualize.htm ...