Springboot+RestTemplate 简单使用
spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可。相较于之前常用的HttpClient,RestTemplate是一种更优雅的调用RESTful服务的方式。该类主要用到的函数有:exchange、getForEntity、postForEntity等。我主要用的是后面两个函数,来执行发送get跟post请求。
首先是RestTemplateUtil类
|
package cn.eangaie.demo.util;
import com.alibaba.fastjson.JSONObject; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate;
import java.util.Map;
/** * @author Eangaie * @date 2018/10/12 0012 下午 14:53 * 网络请求,RestTemplate工具类 */ @Component public
private RestTemplate restTemplate;
/** * 发送GET请求 * @param url * @param param * @return */ public String GetData(String url, Map<String,String> param){ restTemplate=new RestTemplate(); // 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交 HttpHeaders headers = headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); return restTemplate.getForEntity(url,String.class,param).getBody();
}
/** * 发送POST-JSON请求 * @param url * @param param * @return */ public String PostJsonData(String url,JSONObject param){ restTemplate=new RestTemplate(); HttpHeaders headers = headers.setContentType(MediaType.APPLICATION_JSON_UTF8); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<JSONObject> requestEntity = return restTemplate.postForEntity(url,param,String.class).getBody(); }
/** * 发送POST 表单请求 * @param url * @param param * @return */ public String PostFormData(String url,MultiValueMap<String,String> param){ restTemplate=new RestTemplate(); // 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交 HttpHeaders headers = headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); return restTemplate.postForEntity(url,param,String.class).getBody();
} } |
这里编写了三个函数,一个是GetData(), 用来发送GET请求,使用方法是问号传参,并把参数的key作为替代,在map中填入。
PostJsonData ()是用来发送json类型数据的POST请求。需要传入url链接,和一个JSONObject对象。PostFormData()函数是用来发送表单类型
的POST请求。 使用方式我也编写了一些简单的控制器。代码如下。
|
@GetMapping("testGetData") public String testGetData(){ String url="http://localhost:81/sample/GetData?msg={msg}&author={author}"; Map<String,String> param=new HashMap<>(); param.put("msg","Hello"); param.put("author","彦杰"); return restTemplateUtil.GetData(url,param); }
@GetMapping("testPostJsonData") public String testPostJsonData(){ String url="http://localhost:81/sample/PostData"; JSONObject jsonObject=new JSONObject(); jsonObject.put("msg","hello"); jsonObject.put("author","彦杰"); return restTemplateUtil.PostJsonData(url,jsonObject);
}
@GetMapping("testPostFormData") public String testPostFormData(){ String url="http://localhost:81/sample/PostFormData"; MultiValueMap<String,String> param=new LinkedMultiValueMap<>(); param.add("msg","Hello"); param.add("author","彦杰"); return restTemplateUtil.PostFormData(url,param);
}
@GetMapping("GetData") public String getData(String msg, String author){ return msg+" "+author; }
@PostMapping("PostData") public String postData(@RequestBody JSONObject jsonObject){ String msg=jsonObject.getString("msg"); String author=jsonObject.getString("author"); return msg+" "+author; }
@PostMapping("PostFormData") public String PostFormData(String msg,String author){ return msg+" "+author; } @GetMapping("testGetData") public String testGetData(){ String url="http://localhost:81/sample/GetData?msg={msg}&author={author}"; Map<String,String> param=new HashMap<>(); param.put("msg","Hello"); param.put("author","彦杰"); return restTemplateUtil.GetData(url,param); }
@GetMapping("testPostJsonData") public String testPostJsonData(){ String url="http://localhost:81/sample/PostData"; JSONObject jsonObject=new JSONObject(); jsonObject.put("msg","hello"); jsonObject.put("author","彦杰"); return restTemplateUtil.PostJsonData(url,jsonObject);
}
@GetMapping("testPostFormData") public String testPostFormData(){ String url="http://localhost:81/sample/PostFormData"; MultiValueMap<String,String> param=new LinkedMultiValueMap<>(); param.add("msg","Hello"); param.add("author","彦杰"); return restTemplateUtil.PostFormData(url,param);
}
@GetMapping("GetData") public String getData(String msg, String author){ return msg+" "+author; }
@PostMapping("PostData") public String postData(@RequestBody JSONObject jsonObject){ String msg=jsonObject.getString("msg"); String author=jsonObject.getString("author"); return msg+" "+author; }
@PostMapping("PostFormData") public String PostFormData(String msg,String author){ return msg+" "+author; } |
Springboot+RestTemplate 简单使用的更多相关文章
- SpringBoot+RestTemplate 简单包装
RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类. 在SpringBoot中对这个类进行 ...
- springboot+thymeleaf简单使用
关于springboot想必很多人都在使用,由于公司项目一直使用的是SpringMVC,所以自己抽空体验了一下springboot的简单使用. 环境搭建 springbooot的环境搭建可以说很灵活, ...
- Springboot接口简单实现生成MySQL插入语句
Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...
- SpringBoot 搭建简单聊天室
SpringBoot 搭建简单聊天室(queue 点对点) 1.引用 SpringBoot 搭建 WebSocket 链接 https://www.cnblogs.com/yi1036943655/p ...
- SpringBoot 发送简单邮件
使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...
- SpringBoot入门 简单搭建和使用
前言 差不多两年前,那个时候我准备要做毕业设计了,才第一次知道java有框架这种东西,在网上找了好多SSM的教程,那会儿真的是Spring+SpringMVC+MyBatis搭建的,印象极深的是还要写 ...
- Element ui结合springboot的简单实战
Eelment UI简单实战 前端开发 1 创建项目,导入element ui(略) 2 大致设计出想要的效果,如下 3 创建包 根据设计的大致模样在项目的components中创建对应的包,方便以后 ...
- Java Springboot webSocket简单实现,调接口推送消息到客户端socket
Java Springboot webSocket简单实现,调接口推送消息到客户端socket 后台一般作为webSocket服务器,前台作为client.真实场景可能是后台程序在运行时(满足一定条件 ...
- spring-boot RestTemplate 连接池
以前我们项目都是基于Apache HttpClient 连接池进行web 接口调用,后来用spring-boot, 发现 RestTemplate 挺好用. 简单介绍下: 什么是RestTemplat ...
随机推荐
- MicrosoftOfficeProfessionalPlus2013激活方法
MicrosoftOfficeProfessionalPlus2013已经使用很久(估计快一年了吧),但一直是未激活状态,每次打开都要弹出那个未激活的提示信息,很烦,但不知道自己怎么能够忍受这么久的, ...
- Hive中自定义Map/Reduce示例 In Java
Hive支持自定义map与reduce script.接下来我用一个简单的wordcount例子加以说明. 如果自己使用Java开发,需要处理System.in,System,out以及key/val ...
- Golang 并发Groutine实例解读(一)
Go语言的并发和并行 不知道你有没有注意到一个现象,还是这段代码,如果我跑在两个goroutines里面的话: var quit chan int = make(chan int) func loop ...
- [转]LINQ: Using INNER JOIN, Group and SUM
本文转自:https://stackoverflow.com/questions/530925/linq-using-inner-join-group-and-sum SELECT T1.Column ...
- Bootstrap中的datetimepicker插件用法总结(转载)
datetimepicker用法总结 目录 datetimepicker用法总结 目录 简述 官方文档 选项属性 1 format 格式 2 weekStart 一周从哪一天开始 3 startD ...
- why go
取自:http://www.weixinyidu.com/n_3502636 Go的核心贡献者 Go主要有静态语言.天生并发.内置GC.安全性高.语法简单.交叉编译和编译快速这几个方面的特性.这些特性 ...
- proto3 笔记1
定义 Message 类型, 例子如下: syntax = "proto3"; message SearchRequest { string query = 1; int32 pa ...
- 地址解析协议ARP,网络层协议IP、ICMP协议
分析所用软件下载:Wireshark-win32-1.10.2.exe 阅读导览 1. 分析并且应用ARP协议 2.分析IP协议 3.分析ICMP协议 1.分析arp报文的格式与内容 (1)ping ...
- 悟空模式-java-普通工厂模式
[大圣看玩多时,问土地道:“此树有多少株数?”土地道:“有三千六百株.前面一千二百株,花微果小,三千年一熟,人吃了成仙了道,体健身轻.中间一千二百株,层花甘实,六千年一熟,人吃了霞举飞升,长生不老.后 ...
- “使用IDEA,配置文件是yml,无法获取[环境变量],值是null”的问题处理
为了便于隐藏用户名和密码,我们有时会用到“环境变量”. Spring Boot提供了很好的机制,可以在配置文件中,如application.yml书写以下格式,然后在代码中@Value就可以获取“环境 ...