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 提出的一个描述互联系统架构风格 ...
随机推荐
- 旷视MegEngine基本概念
旷视MegEngine基本概念 MegEngine 是基于计算图的深度神经网络学习框架. 本文简要介绍计算图及其相关基本概念,以及它们在 MegEngine 中的实现. 计算图(Computation ...
- 马斯克如何颠覆航天? 1/5385成本,c++和python编程!
马斯克如何颠覆航天? 1/5385成本,c++和python编程! 5月31日,经历了重重困难,马斯克的SpaceX载人飞船成功发射,这是美国自2011年以来首次发射载人航天飞船,也是美国进入由商业主 ...
- 在python_request 中 nb-log 日志模块的使用,应用到项目实际使用
一.安装 pip install nb-log pycham 中安装: 二.基本使用 2.1 pycham中调整设置控制台日志打印出的颜色 2.2 设置完成后去掉console弹出的颜色设置 2.3 ...
- 【NX二次开发】体素特征相关函数(块、柱、锥、球)
NX Open允许用户创建和查询所有基本体素特征,通过API函数建立基本体素特征返回的是相应的特征标识,如果需要可以通过函数UG_MODL_ask_feat_body()获得特征对应的实体对象标识.基 ...
- Java算法面试题(史上最强、持续更新、吐血推荐)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- [apue] linux 文件访问权限那些事儿
前言 说到 linux 上的文件权限,其实我们在说两个实体,一是文件,二是进程.一个进程能不能访问一个文件,其实由三部分内容决定: 文件的所有者.所在的组: 文件对所有者.组用户.其它用户设置的权限访 ...
- Centos8.3、docker部署springboot项目实战记录
引言 目前k8s很是火热,我也特意买了本书去学习了一下,但是k8s动辄都是成百上千的服务器运维,对只有几台服务器的应用来说使用k8s就有点像大炮打蚊子.只有几台服务器的应用运维使用传统的tomc ...
- .net core Redis消息队列中间件【InitQ】
前言 这是一篇拖更很久的博客,不知不觉InitQ在nuget下载量已经过15K了,奈何胸无点墨也不晓得怎么写(懒),随便在github上挂了个md,现在好好唠唠如何在redis里使用队列 队列缓存分布 ...
- 精尽Spring Boot源码分析 - 序言
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- 16、编译安装ansible
16.1.python版本说明: Ansible是一种批量部署工具,现在运维人员用的最多的三种开源集中化管理工具有:puppet,saltstack,ansible,各有各的优缺点, 其中saltst ...