spring mvc绑定参数之日期类型转换

问题复现步骤:

1、提交表单,参数为:

2、接收参数类型为product实体类,如下

 

3、错误日志如下:

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'product' on field 'departureTime': rejected value [2020-02-10 09:00]; codes [typeMismatch.product.departureTime,typeMismatch.departureTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.departureTime,departureTime]; arguments []; default message [departureTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'departureTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-02-10 09:00'; nested exception is java.lang.IllegalArgumentException]

问题分析:

从错误日志中可以看出,是product类的departureTime属性类型从String转换为Date转换错误。

解决方法:

实体类中加日期格式化注解

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
private Date creationTime;

猜你喜欢

转载自www.cnblogs.com/qing-wen/p/12283384.html