当前位置: 首页 > news >正文

java controller Date日期型参数异常的处理方法

介绍

springboot中controller接收Date参数有两种写法,一种是在Date参数上使用注解@DateTimeFormat(pattern=“yyyy-MM-dd HH:mm:ss”),还有一种办法是在controller中添加initBinder。

这两种方法对独立的Date参数或Vo对象中的Date参数都有效,且支持传入null值。

如果不正确处理会报异常

Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'

方法一

第一种,在Date参数上使用注解@DateTimeFormat(pattern=“yyyy-MM-dd HH:mm:ss”)

独立参数写法

@RequestMapping("/test")
public String test(String echo,@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date testDate){
    System.out.println("echo="+echo+",testDate="+testDate);
    return "testDate="+testDate;
}

Vo参数写法

public class EchoVo {
    private String echo;
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date testDate;
    public String getEcho() {
        return echo;
    }
    public void setEcho(String echo) {
        this.echo = echo;
    }
    public Date getTestDate() {
        return testDate;
    }
    public void setTestDate(Date testDate) {
        this.testDate = testDate;
    }
}

方法二

第二种,在controller中添加initBinder

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

独立Date参数示例接口

@RequestMapping("/test")
public String test(String echo, Date testDate){
    System.out.println("echo="+echo+",testDate="+testDate);
    return "testDate="+testDate;
}

Vo对象参数示例接口

@RequestMapping("/testVo")
public String testVo(EchoVo echoVo){
  System.out.println("echo="+echoVo.getEcho()+",testDate="+echoVo.getTestDate());
  return "testDate="+echoVo.getTestDate();
}

在使用SpingMVC框架的项目中,经常会遇到页面某些数据类型是Date、Integer、Double等的数据要绑定到控制器的实体,或者控制器需要接受这些数据,如果这类数据类型不做处理的话将无法绑定。

这里我们可以使用注解@InitBinder来解决这些问题,这样SpingMVC在绑定表单之前,都会先注册这些编辑器。一般会将这些方法些在BaseController中,需要进行这类转换的控制器只需继承BaseController即可。其实Spring提供了很多的实现类,如CustomDateEditor、CustomBooleanEditor、CustomNumberEditor等,基本上是够用的。

相关文章:

  • jpa批量删除 deleteInBatch方式
  • jpa在对主表更新的同时对关联表进行更新操作
  • type: Object, default: 数组/对象的写法
  • 腾讯文档的表格设置行高
  • java order by 防止注入的方法
  • required a bean of type that could not be found. The injection point has the following annotations:
  • 删除换行符在线 在线删除所有回车换行工具
  • 实现将HashMap转换成为ArrayList,并将map的Key 、Value分别存放到两个ArrayList当中
  • java switch可以字符串吗
  • @Query中countQuery的介绍
  • java8 Stream将List转成Map
  • ORDER BY clause is not in GROUP BY clause and contains nonaggregated column ‘xxx.sxxx_.id‘ which is
  • group by 和 where一起使用
  • java求多个Long的平均数,其中可能有null
  • java遍历Map的四种方法
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • Android系统模拟器绘制实现概述
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • Laravel 中的一个后期静态绑定
  • spark本地环境的搭建到运行第一个spark程序
  • Spring Boot快速入门(一):Hello Spring Boot
  • 简单易用的leetcode开发测试工具(npm)
  • 区块链将重新定义世界
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 试着探索高并发下的系统架构面貌
  • 我感觉这是史上最牛的防sql注入方法类
  • 线性表及其算法(java实现)
  • 怎么将电脑中的声音录制成WAV格式
  • Java数据解析之JSON
  • Spring第一个helloWorld
  • 阿里云API、SDK和CLI应用实践方案
  • 仓管云——企业云erp功能有哪些?
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​低代码平台的核心价值与优势
  • !!java web学习笔记(一到五)
  • (02)vite环境变量配置
  • (C#)获取字符编码的类
  • (新)网络工程师考点串讲与真题详解
  • (转)ABI是什么
  • (转)iOS字体
  • .htaccess配置重写url引擎
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • .NET 反射的使用
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • ::什么意思
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • @ResponseBody
  • @Service注解让spring找到你的Service bean
  • @Tag和@Operation标签失效问题。SpringDoc 2.2.0(OpenApi 3)和Spring Boot 3.1.1集成
  • [ 数据结构 - C++]红黑树RBTree
  • [ 云计算 | AWS 实践 ] 基于 Amazon S3 协议搭建个人云存储服务
  • [@Controller]4 详解@ModelAttribute
  • [20150629]简单的加密连接.txt