RestTemplate
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。
内部调用原理
调用RestTemplate的默认构造函数,RestTemplate对象在底层通过使用java.net包下的实现创建HTTP 请求,可以通过使用ClientHttpRequestFactory指定不同的HTTP请求方式。
ClientHttpRequestFactory接口主要提供了两种实现方式:
- SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接
- HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务,使用HttpClient可以配置连接池和证书等信息。
上述内容借用该博客内容:http://blog.csdn.net/lmb55/article/details/70246635
RestTemplate方法讲解
GET方法
- getForEntity
getForEntity函数,返回==ResponseEntity==,该对象是Spring对HTTP请求响应的封装,主要存储了HTTP的几个重要元素,比如HTTP请求状态码的枚举对象==HttpStatus==(常见的404、500等错误码)。在它的父类==HttpEntity==中还存储着HTTP请求的头信息对象==HttpHeaders==以及泛型类型的请求体对象。函数提供了三种重载方式实现:
//方法一
/**
*
* @param url 请求地址
* @param responseType 请求响应体body的包装类型
* @param uriVariables 绑定的参数
* @return
* @throws RestClientException
*/
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables)
throws RestClientException {
...
}
//方法二
/**
*
* @param url 请求地址
* @param responseType 请求响应体body的包装类型
* @param uriVariables map封装参数类型,key-value对应
* @return
* @throws RestClientException
*/
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)
throws RestClientException {
...
}
//方法三
/**
*
* @param url 请求地址
* @param responseType 请求响应体body的包装类型
* @return
* @throws RestClientException
*/
public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException {
...
}
代码演示:
1. ResponseEntity getForEntity(String url, Class responseType, Object… uriVariables);
URL可以采用拼接的方式:
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=test&type=type";
比上述方法更好的方式是采用占位符方式,如下:
String access_token = getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={1}&type={2}";
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<JSONObject> jsonObject = restTemplate.getForEntity(url, JSONObject.class, access_token, "wx_card");
由于uriVariables参数是数组,所以它的顺序会对应url中占位符的数字顺序
- public ResponseEntity getForEntity(String url, Class responseType, Map
String access_token = getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={access_token}&type={type}";
Map<String, String> params = new HashMap<String, String>();
params.put("access_token", access_token);
params.put("type", "wx_card");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<JSONObject> jsonObject = restTemplate.getForEntity(url, JSONObject.class, params);
- ResponseEntity getForEntity(URI url, Class responseType) throws RestClientException;
该方法使用URI对象代替之前的url和uriVariables参数来指定地址和参数绑定。URI是JDK的java.net包下的一个类,它表示一个统一资源标识符(Uniform Resource Identifier)。比如:
String access_token = getAccessToken();
UriComponents uriComponents = UriComponentsBuilder.fromUriString(
"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={access_token}&type={type}")
.build()
.expand(access_token)
.expand("type")
.encode();
URI uri = uriComponents.toUri();
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<JSONObject> jsonObject = restTemplate.getForEntity(uri, JSONObject.class);
关于如何定义一个URI可以参考JDK文档。
找到几个写的不错的博客,URI讲解:http://blog.csdn.net/harvic880925/article/details/44679239
创建URI:http://blog.csdn.net/hermaeuxmora/article/details/51840298
- getForObject
getForObject函数可以理解为对getForEntity的进一步封装,它通过HttpMessageConverterExtractor对HTTP的请求响应体body内容进行对象转换,实现请求直接返回包装好的对象内容。
//方法一
@Override
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {
...
}
//方法二
@Override
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
...
}
//方法三
@Override
public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException {
...
}
POST方法
- -