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 提出的一个描述互联系统架构风格 ...
随机推荐
- Django框架之路由层汇总
一 Django中路由的作用 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来 ...
- Redis系列(一):安装
本系列介绍Redis,从安装到使用,太简单的使用不介绍了,介绍一些比较有意思的功能,也会介绍一些原理性的东西.本篇先介绍Redis的单实例安装.Redis还可以做到高可用,通过哨兵和集群可以做到高可用 ...
- centos 7 显示系统执行的进程
命令:ps -aux ps -aux | more USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 1911 ...
- 透彻理解USB总线应用之枚举
Hello,大家好,今天我们来讨论一下USB总线中的枚举(Enumeration),首先简单介绍一下USB系统的基本架构,它由USB主机.USB设备与USB电缆(本文忽略它)组成,如下图所示: 最常见 ...
- 在vue项目中使用scss,以及vscode适配scss语法(解决使用scss语法编辑器报错)
项目搭建好之后 安装sass 依赖包 npm install --save-dev sass-loader //sass-loader依赖于node-sass npm install --save-d ...
- Java小工具类
计时器(秒表),计算程序运行时间用的 public class Stopwatch { private static long startTime=0; private static long end ...
- 仅使用JsonUtility和File类实现Json数据读写
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using S ...
- Unity的AnimationCurve
转自:风宇冲Unity3D教程学院http://blog.sina.com.cn/s/blog_471132920101f8nv.html,本文有多处增删减改,详细内容请查看原文. 1.介绍 Anim ...
- ORA-12560: 解决TNS:协议适配器错误
1)安装成功,但无法连接数据库 2)网上查找原因:32位的不能运行64位的oracle,而且不会有64位的版本 3)解决办法:大致是修改客户端数据库为32位的(此方法OK) (1)解压instantc ...
- Collections中的实用方法
总结一下java.util.Collections类内部的静态方法. checkedCollection(Collection<T> , Class<T> type) chec ...