首页 > 技术文章 > Field error in object 'student' on field 'date': arguments :nested exception is java.lang.IllegalArgumentException]]

fangweicheng666 2021-08-12 16:32 原文

Field error in object 'student' on field 'date': rejected value [2021-08-12]; codes [typeMismatch.student.date,typeMismatch.date,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [student.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2021-08-12'; nested exception is java.lang.IllegalArgumentException]]

IllegalArgumentException

非法论据异常,也可称为非法形参异常。

原因:浏览器上默认提交的日期默认是String类型,报错400

错误代码:

解决方案:SpringMVC框架提供的@DateTimeFormat注解

@DateTimeFormat把String的日期转成Data日期
pattern属性规定了日期的格式y为年,M为月,d为日

测试效果:

去除时区显示???

方法:@JsonFormat注解

测试效果:

两个注解没有的话,maven坐标如下

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.11.2</version>
    </dependency>
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.10.1</version>
</dependency>

参考:https://blog.csdn.net/qq_44739706/article/details/113901069

推荐阅读