SpringBoot RestTemplate 简单包装解析
作者:谢彦杰 发布时间:2023-02-01 17:02:55
标签:spring,boot,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 class RestTemplateUtil {
private RestTemplate restTemplate;
/**
* 发送GET请求
* @param url
* @param param
* @return
*/
public String GetData(String url, Map<String,String> param){
restTemplate=new RestTemplate();
// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
HttpHeaders headers = new HttpHeaders();
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 = 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;
}
来源:https://www.cnblogs.com/w53064/p/9792301.html
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 在类中自定义的“函数”称为“方法”,由于C#是完全面向对象的
- 1. 栈1.1 概念栈:是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。特点:栈中的数据元素遵循先进后出的原则,但要注意进的
- 1、在ActionSupport中有一个validate()方法,这个方法是验证方法,它会在execute()方法执行之前执行,所以能够起到
- 本文是Neward & Associates的总裁Ted Neward为developerworks独家撰稿“你不知道5个……”系列
- 一、准备官网下载IntelliJ IDEA 2017 并安装好下载汉化包 (链接: https://pan.baidu.com/s/1JkU
- 实际需求<if test="computationRule == '1'"> F
- 本文实例为大家分享了java导出百万以上数据的excel文件,供大家参考,具体内容如下1.传统的导出方式会消耗大量的内存,2003每个she
- 一、新时间日期API常用、重要对象介绍ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则Instan
- 在IDEA中配置log4j,步骤很简单1.在Maven中加入以下配置<dependency> <groupI
- 本Demo使用三个类一个Test类一个自定义的Stack类一个自定义的Queue类可以实现的功能:1.对于一个写在文本文件中的迷宫,能够将其
- 从功能上说,可以分为两部分,分布式功能和数据功能。分布式功能主要是节点集群及集群附属功能如restful借口、集群性能检测功能等,数据功能主
- Java2在1.4中新增了一个关键字:assert。在程序开发过程中使用它创建一个断言(assertion)。,它的语法形式有如下所示的两种
- 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档@Con
- 前言我们在上一篇文章 中以实例讲解如何定义和使用 lambda 表达式,以及与其它语言相比,lambda 表达式在 Java 中的特殊规范。
- MyBatis插入Insert、InsertSelective的区别逆向自动生成的mybatis对应配置Mapper文件里面,有两个方法,分
- 时间处理相关类:1.java.util.Date:时间类2.java.text.DateFormat:时间格式化类(抽象类),实现类:jav
- Timer 详解Timer 和 TimerTask 用于在后台线程中调度任务的 java.
- 需求: 给定一个URL地址, 例如: http://www.cncounter.com/tools/shorturl.php, 解析对应的I
- 前言java处理excel转pdf一直没找到什么好用的免费jar包工具,自己手写的难度,恐怕高级程序员花费一年的事件,也不能做出来非常好用,
- 匿名类类型类可以是匿名的 - 也就是说,可以在没有 identifier 的情况下声明类。在将类名称替换为 typedef 名称时,这会很有