springmvc restTemplate訪問遠端工程

get訪問

        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);

URL中爲訪問的地址,可以加訪問的參數。

String.class即爲返回的值,放回的是一個json值。

post訪問

      當發送的是一個對象的時候:

      String result = restTemplate.postForObject(url, bidPriceRule, String.class);

bidPriceRule即爲一個對象,接受的對象設定的參數必須一致。

    當發送的是其它類型的時候:

        JSONObject data = new JSONObject();
        data.put("type", "1");
        data.put("channelId", Constant.CHANNEL_ID);
        data.put("content", Util.encryEncodeCard(key));
        
        JSONObject result = new JSONObject();
        result = restTemplate.postForObject(path, data, JSONObject.class);

當封裝的是一個list或者類的時候,可以寫成:

       data.put("list",list);

        result = restTemplate.postForObject(path, data.get("list"), JSONObject.class);

MultiValueMap<String, String> mapUser = new LinkedMultiValueMap<String, String>();



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章