spring RestTemplate使用
2017-02-14 00:38
531 查看
最近看到spring RestTemplate,觉得挺好用的,就研究总结了一下,第一次写。。。
RestTemplate可使用http的所有方式进行请求,本文主要说明下get,post的使用,其他的基本类似。
T getForObject(URI url, Class responseType)
T getForObject(String url, Class responseType, MapString< String, ?> urlVariables)
T getForObject(String url, Class responseType, Object… urlVariables)
其中url为请求url,可用通配符表示请求参数,responseType为请求返回的对象类,自动封装成对象该对象形式, Map< String, ?> urlVariables表示请求参数,与通配符对应即可, Object… urlVariables为请求的参数数组形式,按顺序一一匹配url中内容,例子如下:
User 类,省略get,set方法
getForObject
发送方
或
接收方
ResponseEntity getForEntity(String url, Class responseType, Object… urlVariables)
getForEntity返回ResponseEntity,里面包含返回消息内容和http headers,http 状态码,如下例
以上是RestTemplate的get请求使用方式,对于get请求传送head信息的,实现在下面说到
T postForObject(String url, Object request, Class responseType, Object… uriVariables)
T postForObject(String url, Object request, Class responseType, Map
可以看到Object request最终都会转换为HttpEntity,httpEntity类中如下
主要包含headers和body两部分内容,如果Object request可直接转为HttpEntity,则可直接使用,否则默认为HttpEntity中body的内容
参数传递,这种post方式可以避开get方法参数过长的影响,不限制参数长度
或
接收方
传递http head信息,可自行设置http请求的head信息
发送方
接收方
postForLocation返回URI,返回的是response header中的location信息,一般用于资源定位。
ResponseEntity exchange(String url, HttpMethod method,
HttpEntity< ?> requestEntity, Class responseType, Object… uriVariables)
参数设置基本相同,多了个HttpMethod ,设置http的请求方式,通过这个就可以设置Http Get方式的头信息
RestTemplate可使用http的所有方式进行请求,本文主要说明下get,post的使用,其他的基本类似。
http get 方式
spring RestTemplate中直接使用get方法有两种getForObject和getForEntitygetForObject
每种方式都有3个重载方法T getForObject(URI url, Class responseType)
T getForObject(String url, Class responseType, MapString< String, ?> urlVariables)
T getForObject(String url, Class responseType, Object… urlVariables)
其中url为请求url,可用通配符表示请求参数,responseType为请求返回的对象类,自动封装成对象该对象形式, Map< String, ?> urlVariables表示请求参数,与通配符对应即可, Object… urlVariables为请求的参数数组形式,按顺序一一匹配url中内容,例子如下:
User 类,省略get,set方法
public class User { private String name; private Integer age;
private RestTemplate restTemplate = new RestTemplate(); private String name = "xiaoming"; private Integer age = 18;
getForObject
发送方
String url = "http://localhost:8080/getUser?name={name}&age={age}"; Object[] arr = new Object[]{name, age}; User u = restTemplate.getForObject(url, User.class, arr);
或
String url = "http://localhost:8080/getUser?name={name}&age={age}"; Map<String, Object> map = new HashMap<>(); map.put("name", name); map.put("age", age); User u = restTemplate.getForObject(url, User.class, map);
接收方
public User get1(@RequestParam String name, @RequestParam Integer age)
getForEntity
getForEntity与getForObject请求参数基本一样,只是返回内容不一样ResponseEntity getForEntity(String url, Class responseType, Object… urlVariables)
getForEntity返回ResponseEntity,里面包含返回消息内容和http headers,http 状态码,如下例
String url = "http://localhost:8080/getUser?name={name}&age={age}"; Map<String, Object> map = new HashMap<>(); map.put("name", name); map.put("age", age); ResponseEntity<User> res = restTemplate.getForEntity(url, User.class, map); User u = res.getBody(); HttpHeaders headers = res.getHeaders(); HttpStatus status = res.getStatusCode();
以上是RestTemplate的get请求使用方式,对于get请求传送head信息的,实现在下面说到
http post 方式
post方法主要有3种方法:postForObject, postForEntity和postForLocationpostForObject
同样有3种重载T postForObject(String url, Object request, Class responseType, Object… uriVariables)
T postForObject(String url, Object request, Class responseType, Map
private HttpEntityRequestCallback(Object requestBody, Type responseType) { super(responseType); if (requestBody instanceof HttpEntity) { this.requestEntity = (HttpEntity<?>) requestBody; } else if (requestBody != null) { this.requestEntity = new HttpEntity<Object>(requestBody); } else { this.requestEntity = HttpEntity.EMPTY; } }
可以看到Object request最终都会转换为HttpEntity,httpEntity类中如下
public class HttpEntity<T> { /** * The empty {@code HttpEntity}, with no body or headers. */ public static final HttpEntity<?> EMPTY = new HttpEntity<Object>(); private final HttpHeaders headers; private final T body;
主要包含headers和body两部分内容,如果Object request可直接转为HttpEntity,则可直接使用,否则默认为HttpEntity中body的内容
参数传递,这种post方式可以避开get方法参数过长的影响,不限制参数长度
String url = "http://localhost:8080/getUser"; LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add("name", name); map.add("age", age); User u = restTemplate.postForObject(url, map, User.class);
或
String url = "http://localhost:8080/getUser"; LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add("name", name); map.add("age", age); HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map); User u = restTemplate.postForObject(url, httpEntity, User.class);
接收方
public User get1(@ModelAttribute User user)
传递http head信息,可自行设置http请求的head信息
发送方
String url = "http://localhost:8080/getUser"; LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add("name", name); map.add("age", age); HttpHeaders headers = new HttpHeaders(); headers.add("msg", "head msg test"); HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map, headers); User u = restTemplate.postForObject(url, httpEntity, User.class);
接收方
@RequestMapping(value = "/getUser", method = {RequestMethod.POST}) public User get1(@RequestParam String name, @RequestParam Integer age, @RequestHeader(required = false) String msg)
postForEntity和postForLocation
postForEntity返回ResponseEntity,与getForEntity相同postForLocation返回URI,返回的是response header中的location信息,一般用于资源定位。
exchange方法
spring直接提供了所有http的请求的公用方法exchange,简单介绍下ResponseEntity exchange(String url, HttpMethod method,
HttpEntity< ?> requestEntity, Class responseType, Object… uriVariables)
参数设置基本相同,多了个HttpMethod ,设置http的请求方式,通过这个就可以设置Http Get方式的头信息
相关文章推荐
- 使用HttpClient4来构建Spring RestTemplate
- 使用spring RestTemplate发起http请求与远程系统通讯
- 如何使用 Spring RestTemplate
- springboot中配置restTemplate及简单使用
- spring中 使用restTemplate访问rest接口
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务(包括使用 RestTemplate 与 REST 服务进行通信)
- Spring RestTemplate的使用(解决put,delete方法无返回值问题)
- Spring中使用RestTemplate调用第三方接口API
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- 使用spring RestTemplate进行rest接口测试
- spring的RestTemplate的配置及使用
- Spring RestTemplate 配置 SSL https (使用 Spring @MVC)
- 使用RestTemplate Spring安全认证
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- spring rest mvc使用RestTemplate调用
- spring rest mvc使用RestTemplate调用
- spring boot RestTemplate 发送 get 请求使用误区
- 使用HttpClient4来构建Spring RestTemplate
- 解决spring3.0.5使用RestTemplate发送post请求响应中文乱码问题
- Android MVC-spring-android-rest-template使用(转)