在SpringMVC中经过@RequestParam接收jdk8的LocalDateTime时间类型的时候前端
前端经过传入java
后台接收方式web
会出现以下异常:spring
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDateTime] for value '2018-11-11 12:12:12'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-11-11 12:12:12]
解决方式:json
是经过@DateTimeFormat注解,去解析前端传入的时间格式字符串而后转化成LocaDateTime的时间类型ui
那么同时能够说一下spa
@JsonFormat:他和@DateTimeFormat正好对应关系,若是咱们从DB中取得时间类型数据,想传回前端,而且规定格式,那么就能够使用@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")code
pattern:是你须要转换的时间日期的格式orm
timezone:是时间设置为东八区,避免时间在转换中有偏差blog
提示:@JsonFormat注解能够在属性的上方,一样能够在属性对应的get方法上,两种方式没有区别