SpringBoot+RestTemplate 简单包装
RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类。
在SpringBoot中对这个类进行简单的包装,变成一个工具类来使用,这里用到的是getForEntity和postForEntity方法,具体包装的代码内容
如下:
|
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
* 发送GET请求 * @param url * @param param * @return */
restTemplate=new RestTemplate();
HttpHeaders headers = headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
* 发送POST-JSON请求 * @param url * @param param * @return */ public String PostJsonData(String url,JSONObject param){ restTemplate=new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers); 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 = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); return restTemplate.postForEntity(url,param,String.class).getBody();
} } |
在控制类里面调用函数,看看效果
|
@GetMapping("selectUser") public Result selectUser(int id){ user=userService.selectById(id); return ResultUtil.success(user,"查询成功"); }
@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 简单使用
spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值 ...
- 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 ...
随机推荐
- SSH和SSL比较
一.SSH介绍 什么是SSH? 传统的网络服务程序,如:ftp.pop和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据, 别有用心的人非常容易就可以截 获这些口令和数据.而且, ...
- Sql-Server触发器,根据条件匹配另一个表中的字段
USE [CDM] GO /****** Object: Trigger [dbo].[UpdateAkisFlight] Script Date: 2018/6/14 16:43:29 ****** ...
- Java基础教程(19)--Object类
Object类位于类结构树的最顶端,所有的类都是它的直接或间接子类,因此所有的类都继承了Object类的方法,我们可以在需要的时候覆盖这些方法.下面是一些将会在本文中讨论的Object类的方法: ...
- Java生成xlsx格式的excel文件
xlsx格式的写入的数据量据说有百万级,结合实际需要该格式. public static void main(String[] args) throws Exception { OutputStrea ...
- align-items (适用于父类容器上)
align-items (适用于父类容器上) 设置或检索弹性盒子元素在侧轴(纵轴)方向上的对齐方式. 语法 align-items: flex-start | flex-end | center | ...
- bzoj1061 NOI2018 志愿者招募——solution
Description 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难 题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能 ...
- 使用IntelliJ IDEA配置Erlang开发环境
这篇文章比较详细,感谢作者,拷贝过来做个记录 ————————————————————————————————————————————————————————————————————————————— ...
- SystemLogParser ArcGIS Server运维工程师的好助手
单位使用ArcGIS Server发布了众多GIS服务,作为系统运维工程师,一直希望能有一种便捷的方式能获得服务的访问情况,Esri推出的SystemLogParser这款小程序就是不错的辅助工具,而 ...
- 快速 图片颜色转换迁移 Color Transfer Opencv + Python
Super fast color transfer between images About a month ago, I spent a morning down at the beach, w ...
- iOS如何解析crash文件中的地址
1.目录中存放app文件 2.打开文件 3.执行命令otool -arch arm64 -l ./QQStock | grep -B 1 -A 10 "LC_SEGM" | gr ...