![](/img/trans.png)
[英]How to send only the ID the of main nested objects in the body request in spring boot
[英]How to send nested json objects inside a request body
我如何在排球請求的正文中發送此請求,請提前感謝
{
"amount": "000",
"card": {
"number": "00000",
"expiry_month": "00",
"expiry_year": "00",
"cvv": "000",
"pin": "000"
}
}
這是我的請求參數,我已經嘗試過了,這個api告訴我無效的參數,請guzs幫助我
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", User_Token);
headers.put("Content-Type", "application/json");
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
public byte[] getBody() {
Map<String, String> params = new HashMap<String, String>();
params.put( "amount", amount);
params.put( "card", String.valueOf(come()));
return new JSONObject(params).toString().getBytes();
}
private byte[] come() {
Map<String, String> params = new HashMap<String, String>();
params.put( "number", number);
params.put( "expiry_month", month);
params.put( "expiry_year", year);
params.put( "cvv", cvv);
params.put( "pin", pin);
return new JSONObject(params).toString().getBytes();
}
使用JsonObject
代替:創建Map
,將其轉換為Byte[]
,然后獲取其String.valueOf(...)
。 這將使您可以將完整的對象作為請求的正文發送,而不是立即發送的不完整的正文: {"amount":"000","card":"[B@a2e...."}
)
正在為"card"
發送的值存在問題: "[B@a2e...."
是,它不代表對象及其屬性。 而是僅是您創建的字節數組的內存地址。
在您的代碼中,將JsonObject
用於對象,並僅在getBody()
方法末尾執行到Byte[]
的轉換,因為這是您最終返回完整對象的地方:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", User_Token);
// headers.put("Content-Type", "application/json"); // This is probably redundant, as you are already setting it on getBodyContentType()
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
public byte[] getBody() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("amount", amount);
jsonObject.put("card", come());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString().getBytes();
}
private JSONObject come() throws JSONException {
JSONObject params = new JSONObject();
params.put( "number", number);
params.put( "expiry_month", month);
params.put( "expiry_year", year);
params.put( "cvv", cvv);
params.put( "pin", pin);
return params;
}
如果需要精確的格式,則需要確保將number
, month
等值作為字符串而不是整數傳遞。 同樣,為了使代碼看起來更短,破壞性更小,您可以鏈接put
調用。 這是一個重構版本:
@Override
public byte[] getBody() {
try {
final JSONObject card = new JSONObject()
.put("number", String.valueOf(number))
.put("expiry_month", String.valueOf(month))
.put("expiry_year", String.valueOf(year))
.put("cvv", String.valueOf(cvv))
.put("pin", String.valueOf(pin));
return new JSONObject()
.put("amount", String.valueOf(amount))
.put("card", card)
.toString()
.getBytes();
} catch (JSONException e) {
e.printStackTrace();
}
//Empty json return will not help us as in this case
//we will send useless empty body to the server.
return null;
}
我不知道getBody()
調用getBody()
什么,以及您是否能夠修改該代碼。 但是您需要某種方式來跟蹤該null
返回值,或者引入您自己的異常類,該異常類應在getBody()
調用方進行檢查。 但是,如果您無法控制getBody()
調用方,則需要返回一些非null
空返回值:
return new byte[0];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.