![](/img/trans.png)
[英]Send start and end date in requestmapping in rest controller in spring boot
[英]Send anonymous object - Rest controller - Spring boot
與以下 json 匹配的 class 將如何:
'''{
"id1" : {
"apiToCall" : ["200", "400", "500"],
"apiToCall2" : ["100"],
"apiToCall5" : ["600", "300"]
},
"id2" : {
"apiToCall10" : ["300"],
"apiToCall8" : ["600", "700", "500"]
},
"id3" : {
"apiToCall80" : ["200", "400", "500"]
}
}'''
我想使用 spring rest 在 POST 方法中發送這個 object,但我的問題是我有動態屬性。 所以我不知道如何創建管理這種情況的 class 。
HttpEntity<String> entity = new HttpEntity(reqJsonStr, headers);
關鍵點: “HttpEntity<String>”表示請求類型(reqJsonStr)是字符串。
RestTemplate restTemplate = new RestTemplate();
String reqURL = \"https://www.request_url.com/restapi/\";
String reqJsonStr = \"{"
+ " "\"id1\" : {"
+ " \"apiToCall\" : [\"200\", \"400\", \"500\"],"
+ " \"apiToCall2\" : [\"100\"],"
+ " \"apiToCall5\" : [\"600\", \"300\"]"
+ " },"
+ " \"id2\" : {"
+ " \"apiToCall10\" : [\"300\"],"
+ " \"apiToCall8\" : [\"600\", \"700\", \"500\"]"
+ " },"
+ " \"id3\" : {"
+ " \"apiToCall80\" : [\"200\", \"400\", \"500\"]"
+ " }"
+ "}";
headers headers = new headers();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(reqJsonStr, headers);
String responseStr = restTemplate.exchange(reqURL, reqJsonStr, HttpMethod.POST, String.class);
我不確定它是否會起作用,但它可以
Map<String, Map<String, List<String>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.