一、问题:
在做数据更新操作的时候,后台数据为Date时,前端把String类型数据传到后台时,Date类型无法识别这个String数据,所以会报错。文章来源:https://www.toymoban.com/news/detail-592199.html
二、错误描述:
主要问题:
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2019-10-08": not a valid representation (error: Failed to parse Date value '2019-10-08': Unparseable date: "2019-10-08")
at [Source: (PushbackInputStream); line: 1, column: 727] (through reference chain: com.wph.service.edu.entity.Teacher["joinDate"])
三、解决方法:
在实体类中的Data类型数据加上@JsonFormat注解,先来看一下这个注解的属性,我们常用的有timezone{时区}和pattern(图案)文章来源地址https://www.toymoban.com/news/detail-592199.html
部分源码:
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonFormat {
String DEFAULT_LOCALE = "##default";
String DEFAULT_TIMEZONE = "##default";
String pattern() default "";
JsonFormat.Shape shape() default JsonFormat.Shape.ANY;
String locale() default "##default";
String timezone() default "##default";
OptBoolean lenient() default OptBoolean.DEFAULT;
JsonFormat.Feature[] with() default {};
JsonFormat.Feature[] without() default {};
使用:
@JsonFormat(timezone = "UTC",pattern="yyyy-MM-dd")
private Date joinDate;
到了这里,关于解决:Cannot deserialize value of type `java.util.Date` from String “xxx“: not a valid representation..的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!