今天开发项目中,报出了以下的异常,

org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Can not deserialize value of 
type java.util.Date from String "2018-10-17 18:43:02": 
not a valid representation 
(error: Failed to parse Date value '2018-10-17 18:43:02': 
Can not parse date "2018-10-17 18:43:02Z": 
while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'',
 parsing fails (leniency? null)); 
 nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException:
 Can not deserialize value of type java.util.Date from String 
 "2018-10-17 18:43:02": not a valid representation 
 (error: Failed to parse Date value '2018-10-17 18:43:02':
 Can not parse date "2018-10-17 18:43:02Z": 
 while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', 
parsing fails (leniency? null))
复制代码

从异常可以知道,是因为Date字段反序列化过程中,格式非法,导致转换错误。SpringBoot中默认JSON转换是使用Jackson,然后Jackson支持如下几种日期格式。

  • yyyy-MM-dd'T'HH:mm:ss.SSSZ
  • yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
  • yyyy-MM-dd
  • EEE, dd MMM yyyy HH:mm:ss zzz
  • 但是我们常用的是 yyyy-MM-dd HH:mm:ss ,所以需要转换格式。

    我之前已经在配置文件中定义了

    spring:
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8
    复制代码

    所以在使用在写JsonUtil时出现上面的异常,我查了资料,有人建议全局配置

    @Configuration
    public class JacksonConfig {
        @Bean
        public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
            MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
            //设置日期格式
            ObjectMapper objectMapper = new ObjectMapper();
            SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            objectMapper.setDateFormat(smt);
            mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
            return mappingJackson2HttpMessageConverter;
    复制代码

    上述的配置并不能够解决问题,然后我查看源码发现,其实是 ObjectMapper 里面定义了默认格式,所以工具类的时候才把格式转换

    private final static ObjectMapper mapper = new ObjectMapper();
    static {
        mapper.enable(SerializationFeature.WRITE_NULL_MAP_VALUES);
        mapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(format);
    复制代码

    上述配置就解决问题,但是这个并不适用于其他的 ObjectMapper 。试了很多配置都没有效果,最后用了最简单的方式即可解决

    @JsonFormat( pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date createTime;
    复制代码

    直接用注解完美解决。

    安装掘金浏览器插件