restTemplate的用法

 

1.restTemplate.postForObject()方法:POST

HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap paramMap = new LinkedMultiValueMap<>();
paramMap.add("param", param);
HttpEntity> httpEntity = new HttpEntity<>(param, header);
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
String result = restTemplate.postForObject(url, httpEntity, String.class);

 2.restTemplate.exchange ()方法:POST

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity httpEntity = new HttpEntity(json,headers);
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
ResponseEntity resp = restTemplate.exchange(url, HttpMethod.POST,httpEntity,String.class);

3.restTemplate.getForEntity()方法:GET

 String url = "https://www.baidu.com?parm1=aa¶m2=bb¶m3=cc"
 ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class);
 String responseEntity.getBody();

 

 

你可能感兴趣的:(restTemplate的用法)