SpringBoot中的Jackson中日期反序列化問題

楓葉梨花發表於2018-11-12

今天開發專案中,報出了以下的異常,

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;
複製程式碼

直接用註解完美解決。

相關文章