restTemplate常用方法
- restTemplate是spring封装的http请求调用模板
get请求
RestTemplate restTemplate = new RestTemplate();
String url = restTemplate.getForObject("url", String.class);
- 使用UriComponentsBuilder进行参数拼接
RestTemplate restTemplate = new RestTemplate();
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("url")
.queryParam("page", 1)
.queryParam("limit", 10)
.queryParam("param1", "value1");
String resp = restTemplate.getForObject(builder.build().toString(), String.class);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity<>(headers);
ResponseEntity<String> responseEntity = restTemplate.exchange("url", HttpMethod.GET, entity, String.class);
post请求
- 提交表单 APPLICATION_FORM_URLENCODED
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("param1", "value1");
body.add("param2", "value2");
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(body, headers);
String resp = restTemplate.postForObject("url", entity, String.class);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, String> body = new HashMap<>();
body.put("param1", "value1");
body.put("param2", "value2");
HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);
String resp = restTemplate.postForObject("url", entity, String.class);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, String> body = new HashMap<>();
body.put("param1", "value1");
body.put("param2", "value2");
String encryptBody="把body进行加密";
HttpEntity<String> entity = new HttpEntity<>(encryptBody, headers);
restTemplate.getMessageConverters().add(new CustomJacksonHttpMessageConverter());
String resp = restTemplate.postForObject("url", entity, String.class);
自定义消息转换器
- restTemplate默认的是不是解释html返回类型的数据的
- 自定义信息转换器
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.util.Arrays;
public class CustomJacksonHttpMessageConverter extends MappingJackson2HttpMessageConverter {
public CustomJacksonHttpMessageConverter() {
setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN,
MediaType.APPLICATION_JSON, MediaType.TEXT_HTML,
new MediaType("application", "*+json")));
}
}
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, String> body = new HashMap<>();
body.put("param1", "value1");
body.put("param2", "value2");
HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);
restTemplate.getMessageConverters().add(new CustomJacksonHttpMessageConverter());
String resp = restTemplate.postForObject("url", entity, String.class);
springboot项目中使用restTemplate并添加自定义消息转换器
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
RestTemplate build = builder.build();
List<HttpMessageConverter<?>> messageConverters = build.getMessageConverters();
messageConverters.add(new CustomJacksonHttpMessageConverter());
return build;
}
}
|