一、通用方式Exchange(推荐)
1.封装
uriVariables 路径参数
headers header参数
bodyMap body参数
public static ResponseEntity<String> exchange(RestTemplate restTemplate, HttpMethod method, String url, Map<String, Object> uriVariables, HttpHeaders headers, Map<String, Object> bodyMap) {
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(bodyMap, headers);
return restTemplate.exchange(url, method, httpEntity, String.class, uriVariables);
}
2. 调用
以get方式为例,其他方式改变HttpMethod.GET参数
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/other/demo/get?name={name}";
Map<String, Object> uriVariables = new HashMap<>(8);
uriVariables.put("name","test11");
ResponseEntity<String> res = exchange(restTemplate,HttpMethod.GET,url,uriVariables,null,null);
System.out.println(res.getStatusCode());
3.Header参数参考
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "application/json");
headers.set("Token","############");
4.x-www-form-urlencoded形式
注:这里要用MultiValueMap
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
LinkedMultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.put("arg01", Collections.singletonList("111"));
bodyMap.put("arg02", Collections.singletonList(2));
HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(bodyMap, httpHeaders);
ResponseEntity<String> res = restTemplate.exchange(ALERT_UPDATE_URL, HttpMethod.PUT, httpEntity, String.class);
if (200 == res.getStatusCode().value()) {
log.info("成功");
} else {
log.error("失败");
}
二、*ForTemplate和ForObject参考
1.post方式
//有参 请求体【body体中】json参数
JSONObject params=new JSONObject();
params.put("param1","value1");
JSONObject jsonobject=restTemplate.postForTemplate(url,params,JSONObject.class);
//有参 请求体中json参数 设置header头
JSONObject param = new JSONObject();
param.put("param1","value1");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "application/json");
HttpEntity httpEntity = new HttpEntity<>(param, headers);
JSONObject jsonObject = restTemplate.postForObject(url, httpEntity, JSONObject.class);
2.get方式
@Autowired
private RestTemplate restTemplate
String url="https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=3ff9482454cb60bcb73f65c8c48d4209](https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=3ff9482454cb60bcb73f65c8c48d4209)";
//无参 不用指定header的请求
JSONObect jsonobject=restTemplate.getForObject(url,JSONObject.class);
//无参指定header的请求
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
HttpEntity<JSONObject> httpEntity = new HttpEntity<>( headers);
JSONObject jsonobject=restTemplate.getForObject(url,JSONObject.class,httpEntity);
//有参,param方式的参数【参数是拼接在url路径上】
Map<String, Object> params = new HashMap<>();
params.put("param1", "value1");
params.put("param2", "value2");
JSONObject jsonobject=restTemplate.getForObject(url,JSONObject.class,params);
//有参,指定header的请求
String url = "https://....../qwe?sig=......"
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "application/json");
HttpEntity<JSONObject> httpEntity = new HttpEntity(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
String body = exchange.getBody();