【已解决】三种解决方法:Cannot deserialize value of type `java.util.Date` from String
- 工作小总结
- 时间:2022-04-04 09:30
- 9093人已阅读
简介
从redis中获取到数据后,转换对象,报日期转换错
🔔🔔🔔好消息!好消息!🔔🔔🔔
有需要的朋友👉:联系凯哥
json解析使用的是:jackson
从redis中获取到数据后,转换对象,报日期转换错:Cannot deserialize value of type `java.util.Date` from String "2022-04-01 07:42:09": not a valid representation
三种解决方案:
一、改前端
<el-date-picker v-model="formValidate.pastDueTime" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" />
二、改后端实体类
// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date pastDueTime;
三、新增配置类
import java.util.ArrayList; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import com.fasterxml.jackson.databind.ObjectMapper; @Configuration public class WebConfig { @Bean public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); //设置日期格式 ObjectMapper objectMapper = new ObjectMapper(); mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper); //设置中文编码格式 List<MediaType> list = new ArrayList<MediaType>(); list.add(MediaType.APPLICATION_JSON_UTF8); mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list); return mappingJackson2HttpMessageConverter; } }