簡體   English   中英

發送匿名 object - Rest controller - Z38008DD81C2F4D798Z boot ECF6E0CE8AF1D1

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM