SpringBoot中的Jackson中日期反序列化问题

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

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;
复制代码

直接用注解完美解决。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值