发起POST请求-ContentType:APPLICATION_JSON
public void postJson() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, String> map = new HashMap<>();
map.put("a", "a");
map.put("b", "b");
try {
HttpEntity requestParam = new HttpEntity(map, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity("url", requestParam, String.class);
System.out.println(responseEntity);
} catch (RestClientException e) {
e.getMessage();
}
}
发起POST请求-ContentType:MULTIPART_FORM_DATA
@Override
public void postFormData() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("a", "a");
map.add("b", "b");
try {
HttpEntity requestParam = new HttpEntity(map, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity("url", requestParam, String.class);
System.out.println(responseEntity);
} catch (RestClientException e) {
e.getMessage();
}
}