在使用Postman測試Spring Boot項目接口時,接口返回JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String錯誤,如下圖:
參數使用JSON格式,POST請求,如下圖:
createTime變量使用了字符串類型,接口接收參數后將該字符串轉為日期時,發生錯誤,項目中使用fastjson來處理json數據。
此問題解決方法
在接收參數實體類的createtime字段添加JsonFormat注解
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
如下圖:
項目重啟,再次請求,如果如下:
請求成功,如果使用了Timestamp類型來接口,有可能會報下面的異常:
JSON parse error: Can not deserialize value of type java.sql.Timestamp from String
這時,使用上面的方法也可解決問題。