首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >记录 RestTemplate 中的 GET 请求

记录 RestTemplate 中的 GET 请求

作者头像
子乾建建-Jeff
发布2020-06-29 15:19:40
2.1K0
发布2020-06-29 15:19:40
举报
文章被收录于专栏:iBroProiBroPro

RestTemplate 是啥?

它有一个强大的爸爸 Spring。

「虎父无犬子」

Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程 Http 服务的方法,能够大大提高客户端的编写效率。它的堂兄:Http Client。(有了它,堂兄就用的少了)

既然是 Http 服务,那就少不了各种请求。

Show me your code , no can no bb。

在 RestTemplate 中发送 GET 请求:

01

使用 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

02

使用 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();

        StringBuffer sb = new StringBuffer();
        sb.append("body: ").append(body).append("\n statusCodeValue: ").append(statusCodeValue).
                  append("\n headers: ").append(headers).append("\n statusCode: ").append(statusCode);
        return sb.toString();
}

可以看到,ResponseEntity 类型的返回值是 Spring 对 HTTP 请求响应的封装,包括了几个重要的元素,如statusCode、headers、响应消息体等

在 Postman 中查看结果:

其他的使用方法都与 getForObject 大同小异。如果直接只需要返回结果,getForObject 更胜一筹,毕竟越封装,对使用人员越是傻瓜式使用。

读到这里,你对 getForObject 和 getForentity 有了新的认识了吗?

别怕,如果还有些生疏,再亲自敲一遍就可以了。(前提要会搭建一个服务,如果还不会,可以找我)

感谢阅读,感谢陪伴。

表情包来源于网络,侵删。

参考文献:https://blog.csdn.net/u012702547/article/details/77917939

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 iBroPro 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档