一,简介: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请求的更多相关文章

  1. 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)

    Spring 框架的RestTemplate 类定义了一些我们在通过 java 代码调用 Rest 服务时经常需要用到的方法,使得我们通过 java 调用 rest 服务时更加方便.简单.但是 Res ...

  2. Spring RestTemplate get post 请求 携带 headers

    RestTemplate 1.我用RestTemplate请求时 我把他注入到容器里  这样可以 什么用什么时候拿 2.也可以new出来 不过我不喜欢 所以就没有用new的 下面我自己的方法   先注 ...

  3. spring restTemplate 进行http请求的工具类封装

    本文为博主原创,未经允许不得转载: 1.对常用调用的方法进行封装: import org.springframework.http.HttpHeaders; import com.alibaba.fa ...

  4. Spring RestTemplate 之post请求

    ●post请求:在RestTemplate中,POST请求可以通过如下三个方法来发起,但post提交方式又有两种 formData 和 payLoad,而且接口设计与传统的浏览器使用的提交方式又有差异 ...

  5. Spring RestTemplate 小结

    关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...

  6. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  7. Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header

    { "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...

  8. Spring RestTemplate介绍

    http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...

  9. Spring RestTemplate详解

    Spring RestTemplate详解   1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...

随机推荐

  1. 基于Linux的TCP网络聊天室

    1.实验项目名称:基于Linux的TCP网络聊天室 2.实验目的:通过TCP完成多用户群聊和私聊功能. 3.实验过程: 通过socket建立用户连接并传送用户输入的信息,分别来写客户端和服务器端,利用 ...

  2. Spring IOC(控制反转)思想笔记

    Spring IOC(控制反转)思想笔记 IOC控制反转基本理念就是将程序控制权从程序员手中交给用户自定义,从而避免了因为用户一个小需求的变化使得程序员需要改动大量代码. 案例 如果按照之前javaw ...

  3. mybatis中必须使用@param注解的四种情况

    一.方法有多个参数 例如: 接口方法: @Mapper public interface UserMapper { Integer insert(@Param("username" ...

  4. VNC 相关

    vncserver启动报错root A VNC server is already running as :1 [root@42 ~]# service vncserver startStarting ...

  5. 【VBA】模块更新方法

    删除模块,重新导入 1 Sub 更新模块() 2 With ThisWorkbook.VBProject 3 .VBComponents.Remove .VBComponents("模块1& ...

  6. Air530Z GPS/北斗定位模块_设计指导手册_V1.2

    下载PDF版本: Air530Z_定位模块_设计指导手册_V1.2.pdf @ 目录 1. 模块整体说明 2. 资料下载 3. 模块性能 4.模块管脚图 5.参考设计电路 6.GPS天线 6.1 无源 ...

  7. Java synchronized对象级别与类级别的同步锁

    Java synchronized 关键字 可以将一个代码块或一个方法标记为同步代码块.同步代码块是指同一时间只能有一个线程执行的代码,并且执行该代码的线程持有同步锁.synchronized关键字可 ...

  8. Mybatis数据连接池的配置---增删改查(以及遇见的问题)

    1.首先创建项目和各个文件,如图所示: 2.配置相关数据库连接 在jdbc.properties中加入 1 db.driver=com.mysql.jdbc.Driver 2 db.url=jdbc: ...

  9. 【题解】Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo

    Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo 题目描述 Farmer John and Bessie the cow love to exchange ma ...

  10. LM-MLC 一种基于完型填空的多标签分类算法

    LM-MLC 一种基于完型填空的多标签分类算法 1 前言 本文主要介绍本人在全球人工智能技术创新大赛[赛道一]设计的一种基于完型填空(模板)的多标签分类算法:LM-MLC,该算法拟合能力很强能感知标签 ...