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 ...
随机推荐
- Abp中SwaggerUI的多个接口文档配置说明
对外提供的接口在实际生成过程中,可能是需要一个接口版本的,比如说v1,manage.效果如下: 在swagger中怎么实现呢? 1. 添加SwaggerVersionHelper.cs pub ...
- Node.js构建可扩展的Web应用1
<Practical Node.js:Building Real-World Scalable Web Apps>[美]Azat Mardan(电子工业出版社) 安装node.js和NPM ...
- Javascript Madness: Mouse Events
http://unixpapa.com/js/mouse.html Javascript Madness: Mouse Events Jan WolterAug 12, 2011 Note: I ha ...
- 算法:QQ等级换算成皇冠太阳星星月亮
/// <summary> /// 等级换算成图标分布 /// 以QQ的形式计算 /// 2^(2*0) /1 /// 2^(2*1) /4 /// 2^(2*2) /16 ...
- 前端(十):使用redux管理数据
react本身能够完成动态数据的监听和更新,如果不是必要可以不适用redux. 安装redux: cnpm install redux --save,或者yarn add redux. 一.react ...
- HTML列表(组标签)+div(布局标签)与span
一.列表 HTML中常见的列表有三种,分别是: 1.无序列表,是一组描述列表语义的组标签,列表中每个项之间没有先后顺序:如图: 1)组标签:组标签就是由多个标签组成的一个整体,它们之间共同存在:例如 ...
- 如何在PIXI.js里面使用json文件来管理瓦片集(tileset)?
如何在PIXI.js里面使用json文件来管理瓦片集(tileset)? PIXI建议我们将素材图片汇总成一个瓦片集(tileset),然后用纹理地图集(texture atlas,通常是一个json ...
- 深入理解MyBatis的原理(三):配置文件用法(续)
前言:前文讲解了 MyBatis 的配置文件一部分用法,本文将继续讲解 MyBatis 的配置文件的用法. 目录 1.typeHandler 类型处理器 2.ObjectFactory 3.插件 4. ...
- 区分IE8/IE7/IE6及其他浏览器-CSS “\9″
区分IE8/IE7/IE6及其他浏览器-CSS “\9″ 原创文章,转载请注明来自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com] by zhangxinxu from h ...
- Spring Hibernate JPA 联表查询 复杂查询
今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...