RestTemplat是Spring3.0开始提供的http请求的工具,提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率,RestTemplate 是一个同步的 Rest API 客户端;
通过SimpleClientHttpRequestFactory工厂统一设置连接超时时间和读超时时间、代理等信息,然后创建RestTemplate的Bean;
还可以通过SimpleClientHttpRequestFactory工厂配置http请求连接数,忽略ssl证书等;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory simpleClientHttpRequestFactory) {
return new RestTemplate(simpleClientHttpRequestFactory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(10000);//单位为ms
factory.setConnectTimeout(10000);//单位为ms
//factory.setProxy(null);//设置代理
return factory;
}
}
http method | 对应方法 |
---|---|
post | postForEntity和postForObject |
get | getForEntity和getForObject |
put | put |
delete | delete |
Head | headForHeaders |
options | optionsForAllow |
options | optionsForAllow |
1:exchange:可以自己设置http method、headers、url,传入RequestEntity,返回ResponseEntity
2:execute:通过callback,对请求和返回更好的控制;但是不常用
JSONObject jsonObject = new JSONObject();
jsonObject.put("payOrderNo", payOrderNo);
jsonObject.put("refundNo", refundNo);
jsonObject.put("refundAmount", refundAmount.multiply(new BigDecimal(100)));
jsonObject.put("refundReason", refundReason);
HttpEntity httpEntity= new HttpEntity<>(jsonObject);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.add("X-App-Id", appId);
headers.add("X-Time-Stamp", map.get("X-Time-Stamp"));
HttpEntity requestParam = new HttpEntity<>(jsonObject, headers);
//post请求返回ResponseEntity
ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, JSONObject.class);
//post请求返回指定的类型
JSONObject responseEntity = restTemplate.postForObject(urls, httpEntity, JSONObject.class);
//get请求返回ResponseEntity
ResponseEntity responseEntity = restTemplate.getForEntity(urls, JSONObject.class);
//get请求返回指定的类型
JSONObject responseEntity = restTemplate.getForObject(urls, JSONObject.class);
ResponseEntity responseEntity = restTemplate.exchange(url,HttpMethod.POST,httpEntity, JSONObject.class);
首先创建拦截器类并实现ClientHttpRequestInterceptor 接口
@Slf4j
public class HTTPInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
log.info("执行拦截器拦截请求{}",httpRequest.getURI());
return clientHttpRequestExecution.execute(httpRequest,bytes);
}
}
在RestTemplate创建bean的时候添加拦截器
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory simpleClientHttpRequestFactory) {
RestTemplate restTemplate= new RestTemplate(simpleClientHttpRequestFactory);
List list=new ArrayList<>();
list.add(new HTTPInterceptor());
restTemplate.setInterceptors(list);
return restTemplate;
}
这样客户端在发起http请求的时候就会执行拦截器
通过 RestTemplate,我们可以非常方便的进行 Rest API 调用。但是在 Spring 5 中已经不再建议使用 RestTemplate,而是建议使用 WebClient。WebClient 是一个支持异步调用的 Client。