Spring RestTemplate 之get请求
一,简介:Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率
二、RestTemplate中几种常见请求方法的使用
●get请求:在RestTemplate中,发送一个GET请求,我们可以通过如下两种方式
第一种:getForEntity
getForEntity方法的返回值是一个ResponseEntity<T>,ResponseEntity<T>是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。例子:
@Controller
@RequestMapping("/restTest")
public class RestTempLateTest {
private RestTemplate restTemplate = new RestTemplate();
@RequestMapping("/hello")
@ResponseBody
public String getHello() {
// ResponseEntity<IntMonitor> res = restTemplate.getForEntity(url,
// IntMonitor)
ResponseEntity<String> res = restTemplate.getForEntity(
"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor",
String.class);
String body = res.getBody();
return body;
}
}
有时候我在调用服务提供者提供的接口时,可能需要传递参数,有两种不同的方式,如下
@RequestMapping("/hello1/{flag}")
@ResponseBody
public String getHello1(@PathVariable String flag){
ResponseEntity<String> res = restTemplate.getForEntity(
"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{1}",
String.class,
"1");
String body = res.getBody();
return body;
}
@RequestMapping("/hello2/{flag}")
@ResponseBody
public String getHello2(@PathVariable String flag){
Map<String, Object> map = new HashMap<String, Object>();
map.put("flag", flag);
ResponseEntity<String> res = restTemplate.getForEntity(
"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}",
String.class,
map);
String body = res.getBody();
return body;
}
@RequestMapping("/hello3/{flag}")
@ResponseBody
public String getHello3(@PathVariable String flag) {
UriComponents uriComponents = UriComponentsBuilder
.fromUriString(
"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}")
.build().expand(flag);
URI uri = uriComponents.toUri();
ResponseEntity<String> res = restTemplate.getForEntity(uri, String.class);
String body = res.getBody();
return body;
}
可以用一个数字做占位符,最后是一个可变长度的参数,来一一替换前面的占位符
也可以前面使用name={name}这种形式,最后一个参数是一个map,map的key即为前边占位符的名字,map的value为参数值
调用地址也可以是一个url,而不是一个字符串,这样可以直接调用url.
第二种:getForObject
getForObject函数实际上是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject,
举一个简单的例子,如下:
@RequestMapping("/hello4/{flag}")
@ResponseBody
public String getHello4(@PathVariable String flag) {
UriComponents uriComponents = UriComponentsBuilder
.fromUriString(
"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}")
.build().expand(flag);
URI uri = uriComponents.toUri();
String res = restTemplate.getForObject(uri, String.class);
return res;
}
Spring RestTemplate 之get请求的更多相关文章
- 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)
Spring 框架的RestTemplate 类定义了一些我们在通过 java 代码调用 Rest 服务时经常需要用到的方法,使得我们通过 java 调用 rest 服务时更加方便.简单.但是 Res ...
- Spring RestTemplate get post 请求 携带 headers
RestTemplate 1.我用RestTemplate请求时 我把他注入到容器里 这样可以 什么用什么时候拿 2.也可以new出来 不过我不喜欢 所以就没有用new的 下面我自己的方法 先注 ...
- spring restTemplate 进行http请求的工具类封装
本文为博主原创,未经允许不得转载: 1.对常用调用的方法进行封装: import org.springframework.http.HttpHeaders; import com.alibaba.fa ...
- Spring RestTemplate 之post请求
●post请求:在RestTemplate中,POST请求可以通过如下三个方法来发起,但post提交方式又有两种 formData 和 payLoad,而且接口设计与传统的浏览器使用的提交方式又有差异 ...
- Spring RestTemplate 小结
关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明 ...
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header
{ "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...
- Spring RestTemplate介绍
http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...
- Spring RestTemplate详解
Spring RestTemplate详解 1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...
随机推荐
- YOLOv4 资源环境配置和测试样例效果
YOLOv4 资源环境配置和测试样例效果 基本环境:cuda=10.0,cudnn>=7.0, opencv>=2.4 一.下载yolov4 git clone https://githu ...
- TinyML-TVM是如何驯服Tiny的(下)
TinyML-TVM是如何驯服Tiny的(下) Lazy Execution实际上,随着通信开销开始占主导地位,一旦用户请求,就执行算子的开销变得非常昂贵.可以通过延迟评估直到用户需要调用的结果来提高 ...
- selenium 隐式等待与显式等待
1.隐式等待:driver.implicitly_wait() driver = webdriver.Chrome()driver.implicitly_wait(10) #获取元素时最多会等 ...
- ABAP SORT排序注意点
SORT TABLE BY XXX XXX .如果不加任何语法都是默认升序,延伸可以用ASCENDING和DESCENDING对具体的字段进行升序和降序排列. 简单通俗的来阐释一下,在字段名后面加AS ...
- 实验4、Flask基于Blueprint & Bootstrap布局的应用服务
1. 实验内容 模块化工程内容能够更好的与项目组内成员合作,Flask Blueprint提供了重要的模块化功能,使得开发过程更加清晰便利.此外,Flask也支持Bootstrap的使用. 2. 实验 ...
- 小程序微信支付(UNIAPP+第三方SDK:binarywang)
小程序支付流程图说明(UNIAPP+第三方SDK:binarywang) 说明:小程序为UNI-APP开发,使用的第三方微信支付SDK为binarywang提供的,此SDK对微信公众号.小程序.微信各 ...
- Effective Fusion Factor in FPN for Tiny Object Detection
微小目标检测的FPN有效融合因子 摘要:基于FPN的检测器在一般物体检测方面取得了显著的进步,例如MS COCO和PASCAL VOC.然而,这些检测器在某些应用场景中会失败,例如微小物体检测.在本文 ...
- Netty 面试题 (史上最全、持续更新)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- Mybatis中9种经典的设计模式!你知道几个?
虽然我们都知道有23个设计模式,但是大多停留在概念层面,真实开发中很少遇到.Mybatis源码中使用了大量的设计模式,阅读源码并观察设计模式在其中的应用,能够更深入的理解设计模式. Mybatis至少 ...
- 【linux】驱动-14-异步通知
目录 前言 14. 异步通知 14.1 异步通知的一些概念 14.2 Linux 信号 14.3 信号接收 14.4 使用流程 14.4.1 参考流程图 14.4.2 分析&编程步骤 14.4 ...