说一说 RestTemplate 中的 GET 请求
一 RestTemplate 是啥?
它有一个强大的爸爸 Spring。
Spring 提供的用于访问 Rest 服务 的客户端,RestTemplate 提供了多种便捷访问远程 Http 服务的方法,能够大大提高客户端的编写效率。它的堂兄: Http Client 。 (有了它,堂兄就用的少了)
既然是 Http 服务,那就少不了各种请求。
Show me your code , no can no bb。
在 RestTemplate 中发送 GET 请求:
二 使用 getForObject()
先来看下 getForObject 的 API 说明:
getForObject(String url, Class<T> responseType, Object... uriVariables) T
参数说明:
- String url : 调用的服务的 url
- Class<T> responsetype : 返回对象的类型
- Object... uriVariables : url 中的可变参数(配合占位符使用)
- T :返回类型,泛型
getForObject(URI url, Class<T> responseType) T
参数说明:
- URI url : URI 类型的地址
- Class<T> responsetype : 返回对象的类型
- T : 返回类型,泛型
getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) T
参数说明:
- String url : 调用的服务的 url
- Class<T> responsetype : 返回对象的类型
- Map<String, ?> uriVariables : url 中的可变参数(配合占位符使用)
- T : 返回类型,泛型
1 传入一个值,返回一个对象的情况:
(列举了常用的,没有面面俱到,可以照猫画虎)
1.1 直接将变量写在 url 中,记得添加注解 @PathVariable
// 参数拼接
@GetMapping("/user/{id}")
public User findUserById(@PathVariable Long id){
return restTemplate.getForObject("http://localhost:8000/user/"+id, User.class);
}
这里的 User.class 即代表返回的参数类型。
1.2 将变量通过key=word形式传递,
通过 HttpServletRequest 获取参数
// 地址栏 key=value 形式传参
@GetMapping("/req")
public User findAUser(HttpServletRequest request){
String id = request.getParameter("id");
return restTemplate.getForObject("http://localhost:8000/req?id="+id, User.class);
}
结果:
1.3 通过占位符:
// 参数的不同传法 数字占位符
@GetMapping("/one/{id}")
public User findOneUser(@PathVariable Long id){
return restTemplate.getForObject("http://localhost:8000/user/{0}",User.class, id);
}
{0} 的具体值来自于 id 。
其实我试了花括号里面任意的值都可以,因为现在仅传递了一个参数。
1.4 通过占位符,结合 Map:
// 参数的不同传法,Map类型
@GetMapping("/one/{id}")
public User findOneUser(@PathVariable Long id){
Map<String, Long> map = new HashMap<String, Long>();
map.put("id",id);
return restTemplate.getForObject("http://localhost:8000/user/{id}", User.class, map);
}
更适合有多个参数的情况。
花括号里面的名字值和 map 的键名字保持一致即可,就可以传递对应的值。
1.5 通过 URI 进行 访问:
@GetMapping("/req")
public User findAUser(HttpServletRequest request){
String id = request.getParameter("id");
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:8000/req?id="+id).
build().encode();
URI uri = uriComponents.toUri();
return restTemplate.getForObject(uri, User.class);
}
使用 Spring 的 UriComponents 工具,参数可以整合到路径中。
▌一种方法可以直接拼接,如上图。
▌另外一种可以利用占位符,通过 expand() 方法实现。如下:
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:8000/req?id={id}").
build().expand(id).encode();
.build.expand(id).encode 。
2 传入一个值,返回一个列表的情况:
尝试使用 List<User> 作为返回类型,编译阶段就报错。改为 User[] 数组。
// 返回多个对象(jpa中是list列表)
@GetMapping("/users")
public User[] findAllUser(){
return restTemplate.getForObject("http://localhost:8000/users", User[].class);
}
注意函数的第二个参数 User[].class 。
三 使用 getForEntity()
也是有 3 个重载的方法,和 getForObject 一样。( 不对,应该说 getForObject 和它一样,getForObject 是对它的封装 )
为什么呢?看它的返回值,是 ResponseEntity<T> 。
通过代码来探讨一下:
@GetMapping("/user/{id}")
public ResponseEntity<String> findUserById(@PathVariable Long id){
//return restTemplate.getForObject("http://localhost:8000/user/"+id, User.class);
return restTemplate.getForEntity("http://localhost:8000/user/"+id, String.class);
}
函数中第二个参数 String.class ,必须和 ResponseEntity<T> 中的 T 保持一致。也可以设置成 User.class。T 为 User。
在 Postman 中结果是这样的:
看上去没有什么异同。这是因为工具直接封装,直接拿到了 Json 类型 的数据。稍作改变:
@GetMapping("/user/{id}")
public String findUserById(@PathVariable Long id){
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8000/user/"+id,
String.class);
String body = responseEntity.getBody();
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders headers = responseEntity.getHeaders();
StringBuffer result = new StringBuffer();