简体   繁体   中英

Send anonymous object - Rest controller - Spring boot

How would be the class that matches the following json:

   '''{
    "id1" : {
        "apiToCall" : ["200", "400", "500"],
        "apiToCall2" : ["100"],
        "apiToCall5" : ["600", "300"]
    },
    "id2" : {
        "apiToCall10" : ["300"],
        "apiToCall8" : ["600", "700", "500"]
    },
    "id3" : {
        "apiToCall80" : ["200", "400", "500"]
    }
}'''

I want to send this object in POST method using spring rest, but my issue is that i have dynamic attributes. So i don't know how to create the class that manages this case.

HttpEntity<String> entity = new HttpEntity(reqJsonStr, headers);

KeyPoint: "HttpEntity<String>" means type of request(reqJsonStr) is string.

    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);

I am not sure if it will work or not but it can be

 Map<String, Map<String, List<String>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM