使用RestTemplate发送POST和GET请求
发送POST请求,JSON
RestTemplate restTemplate = new RestTemplate();
JSONObject param = new JSONObject();
param.put("phone", "15625603321");
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
header.add("authorization", "xxx");
HttpEntity<JSONObject> httpEntity = new HttpEntity<>(param, header);
JSONObject res = restTemplate.postForObject(url, httpEntity, JSONObject.class);
发送POST请求,表单
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("client_id", "aaaa");
params.add("client_secret", "xxxx");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(params, headers);
JSONObject entity = restTemplate.postForObject(url, formEntity, JSONObject.class);
发送GET请求
String res = restTemplate.getForObject(url, String.class);