相关文章推荐
逆袭的小笼包  ·  golang 把内容写到 csv 文件或者 ...·  1 年前    · 
正直的芹菜  ·  MySQL :: MySQL 8.0 ...·  1 年前    · 
豪情万千的沙滩裤  ·  未解析的引用print怎么解决-掘金·  1 年前    · 
酒量大的充电器  ·  解决svg中使用渐变色和阴影导致路径不显示的 ...·  2 年前    · 
行走的丝瓜  ·  进制表示 ...·  2 年前    · 
Code  ›  记录 RestTemplate 中的 GET 请求开发者社区
string resttemplate
https://cloud.tencent.com/developer/article/1652303
仗义的刺猬
2 年前
作者头像
子乾建建-Jeff
0 篇文章

记录 RestTemplate 中的 GET 请求

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > iBroPro > 记录 RestTemplate 中的 GET 请求

记录 RestTemplate 中的 GET 请求

作者头像
子乾建建-Jeff
发布 于 2020-06-29 15:19:40
1.5K 0
发布 于 2020-06-29 15:19:40
举报

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();
 
推荐文章
逆袭的小笼包  ·  golang 把内容写到 csv 文件或者 xlsx 文件里_51CTO博客_golang读取csv文件列数据
1 年前
正直的芹菜  ·  MySQL :: MySQL 8.0 Reference Manual :: 15.2.13 SELECT Statement
1 年前
豪情万千的沙滩裤  ·  未解析的引用print怎么解决-掘金
1 年前
酒量大的充电器  ·  解决svg中使用渐变色和阴影导致路径不显示的问题_svg path 渐变色-CSDN博客
2 年前
行走的丝瓜  ·  进制表示 Verilog:实现二进制、十进制和十六进制数的转换_verilog 十六进制-CSDN博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号