我正在創建一個呼叫外部服務的 Rest 服務。我有這個問題:我呼叫的外部服務根據成功或失敗的情況以兩種方式回應。這是json:
json{
"result": "001",
"status": "Success",
"response": {
"codiceCase": "CAS-46759-Q8P7X3",
"guidCase": "88458d32-dd42-ec11-8c62-0022489d2f61"}
}
OR
{
"result": "002",
"status": "Failed",
"errorManagement": {
"errorCode": "E02",
"errorDescription": "field not value in body"
}
}
好吧,我創建了 3 個簡單的類:
- class XXX...私有字串結果,狀態;.... 吸氣劑和吸氣劑
- 類回應...私有字串 codiceCase,guidCase;... 吸氣劑和吸氣劑
- class ErrorManagement...private String errorCode, errorDescription;... getter & setter 但是當我用我的模擬填充時,json 總是由我不關心的類欄位形成,例如:
{
"result": "001",
"status": "Success",
"response": {
"codiceCase": "CAS-46759-Q8P7X3",
"guidCase": "88458d32-dd42-ec11-8c62-0022489d2f61" },
{
***"errorMessage"**: null}
}
我怎樣才能在我的模擬中只得到 3 個類中的 2 個作為 json 回傳?謝謝你的幫助。
uj5u.com熱心網友回復:
我想您正在使用 Jackson 反序列化 JSON,因為這是 - 我認為 - spring 的默認設定。
使用 Jackson,您可以注釋模型類以省略nullJSON 字串表示中的值:
@JsonInclude(Include.NON_NULL)
public class Model { … }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/379582.html
