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