【问题标题】:Send anonymous object - Rest controller - Spring boot发送匿名对象 - 休息控制器 - 春季启动
【发布时间】:2021-05-27 21:43:06
【问题描述】:

如何匹配以下 json 的类:

   '''{
    "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 方法中发送这个对象,但我的问题是我有动态属性。 所以我不知道如何创建管理这种情况的类。

【问题讨论】:

    标签: java spring spring-boot spring-restcontroller spring-rest


    【解决方案1】:

    HttpEntity entity = new HttpEntity(reqJsonStr, headers);

    KeyPoint : "HttpEntity" 表示请求类型(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);
    

    【讨论】:

      【解决方案2】:

      我不确定它是否会起作用,但它可以是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-14
        • 2015-06-30
        • 2023-03-25
        • 2020-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-25
        相关资源
        最近更新 更多