問題:
在使用Postman測試Spring Boot項目接口時,接口返回JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String錯誤,記錄如下:
Postman參數如圖
接收對象參數如圖:
報錯:
JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String
如圖:
解決方法
在接收參數實體類的createtime字段添加JsonFormat注解
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
如圖:
注意
請求成功,如果使用了Timestamp類型來接口,有可能會報下面的異常:
JSON parse error: Can not deserialize value of type java.sql.Timestamp from String
這時,使用上面的方法也可解決問題。