1. 程式人生 > >實際開發中String轉換為json串作為入參發生"JSON parse error:Cannot deserialize value of type Date......not a valid解決

實際開發中String轉換為json串作為入參發生"JSON parse error:Cannot deserialize value of type Date......not a valid解決

實際開發中,String拼接成json串作為入參請求介面,發生以下錯誤

{
    "timestamp": "2018-11-09 14:55:49",
    "status": 400,
    "error": "Bad Request",
    "message": "JSON parse error: Cannot deserialize value of type `java.util.Date` from String \"2016-02-02\": not a valid representation 
    (error: Failed to parse Date value '2016-02-02': Unparseable date: \"2016-02-02\"); 
    nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: 
    Cannot deserialize value of type `java.util.Date` from String \"2016-02-02\": not a valid representation 
    (error: Failed to parse Date value '2016-02-02': Unparseable date: \"2016-02-02\")\n at [Source: (PushbackInputStream); 
    line: 89, column: 23] (through reference chain: com.guorenpcic.grecar.dto.vo.Proposal[\"bc\"]->com.guorenpcic.grecar.dto.vo.BCVo[\"prpTitemCar\"]->
    com.×××.grecar.dto.entity.Prptitemcar[\"certifiCateDate\"])",
    "path": "/ecar/calculationPremium"
}

【問題】這是該字串certifiCateDate作為Date格式的無法轉換成json,轉換錯誤。

【解決辦法】實體Prptitemcar欄位certifiCateDate上新增 @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") 註解既解決。