赞
踩
项目地址:https://gitee.com/firewolf/open-utils/tree/master/RestTemplate
RestTemplate是Spring提供的一个类似于HTTPClient的用于模拟http请求模板工具类,在Spring项目中,如果我们需要发起Http请求,可以很方便的使用这个工具类进行处理
GET请求有两类:getForObject和getForEntity,其中getForObject返回的是响应体,而getForEntity返回的包括:响应行、响应头、响应体,所以这里只讲解getForObject,而忽略到getForObject
public <T> T getForObject(URI url, Class<T> responseType)
: 发起get请求,一般用于没有请求参数的时候。http://localhost:8080/user?name=zhangsan&age=123
。public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
:使用Map传递参数 Map<String, Object> args = new HashMap<>();
args.put("name", name);
args.put("age", age);
args.put("hobby", StringUtils.join(hobby, ","));
restTemplate.getForObject(USER_URL_GET + "/2?name={name}&age={age}&hobby={hobby}", ResponseVO.class, args);
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables)
: 和上面的方法类似,不同的是需要把所有的参数跟在后面的可变参数中,没有参数的时候也可以。restTemplate
.getForObject(USER_URL_GET + "/2?name={name}&age={age}&hobby={hobby}", ResponseVO.class, name, age,
StringUtils.join(hobby, ","));
和GET请求一样,POST请求也有postForObject已经postForEntity两种,区别也在于前者只返回响应体,后者会返回响应头、响应行、响应体
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables)
:发起POST请求,各个参数含义如下: ApiUser u = new ApiUser();
u.setHobby(hobby);
u.setAge(age);
u.setName(name);
return restTemplate.postForObject(USER_URL_POST + "/1", u, ResponseVO.class);
接受方为:
@PostMapping("/1")
public ResponseVO addUser(@RequestBody User user) {
log.info("user-post 1 args: {}", user);
return ResponseVO.ok();
}
如果使用url+uriVariables来传递参数,那么在接受方不需要使用注解来接受参数,而且,这种方式传递数组需要转成,拼接的字符串;
如:
Map<String, Object> map = new HashMap<>();
map.put("name", name);
map.put("age", age);
map.put("hobby", StringUtils.join(hobby,","));
return restTemplate
.postForObject(USER_URL_POST + "/2?name={name}&age={age}&hobby={hobby}", null, ResponseVO.class, map);
接受方为:
@PostMapping("/2")
public ResponseVO addUser2(User user) {
log.info("user-post 2 args: {}", user);
return ResponseVO.ok();
}
2.public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables)
:这个方法和上面的类似,区别在于可以不传递参数。
//设置请求数据的格式
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//封装参数
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("name", name);
params.add("age", age);
hobby.forEach(x->params.add("hobby",x)); //添加多值,或者是使用,分割的字符串传递数组参数
//封装请求内容
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
return restTemplate
.postForObject(USER_URL_POST + "/2", requestEntity, ResponseVO.class);
接受方:
@PostMapping("/2")
public ResponseVO addUser2(User user) {
log.info("user-post 2 args: {}", user);
return ResponseVO.ok();
}
put请求没有返回参数,有三个方法
public void put(String url, @Nullable Object request, Object... uriVariables)
ApiUser u = new ApiUser();
u.setHobby(hobby);
u.setAge(age);
u.setName(name);
restTemplate.put(USER_URL_PUT + "/1", u);
return ResponseVO.ok();
接受方:
@PutMapping("/1")
public void updateUser(@RequestBody User user) {
log.info("user-post 1 args: {}", user);
}
b.使用url+uriVariables传递参数,那么后台直接使用对象接受参数
@PostMapping("/2")
public ResponseVO putWithURLArgs(String name, Integer age, @RequestParam ArrayList<String> hobby) {
//这种方式传递数组需要用,拼接成字符串
restTemplate.put(USER_URL_PUT + "/2?name={name}&age={age}&hobby={hobby}", null, name, age, StringUtils.join(hobby,","));
return ResponseVO.ok();
}
接受方:
@PutMapping("/2")
public void updateUser2(User user) {
log.info("user-post 2 args: {}", user);
}
public void delete(String url, Map<String, ?> uriVariables)
public void delete(String url, Object... uriVariables)
使用exchange方法可以方便的发起各种请求,方法比较多,但是大同小异,这里以其中一个方法举例说明用法。
exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)
@PostMapping("/1")
public ResponseVO postWithPojoArgs(String name, Integer age, @RequestParam ArrayList<String> hobby) {
ApiUser u = new ApiUser();
u.setHobby(hobby);
u.setAge(age);
u.setName(name);
HttpEntity<ApiUser> httpEntity = new HttpEntity<>(u);
ResponseEntity<ResponseVO> exchange = restTemplate
.exchange(USER_URL_EXCHANGE + "/1", HttpMethod.POST, httpEntity, ResponseVO.class);
return exchange.getBody();
}
接收方:
@PostMapping("/1") public ResponseVO addUser(@RequestBody User user) { log.info("user-post 1 args: {}", user); return ResponseVO.ok(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。