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;

}

     

SpringBoot+RestTemplate 简单包装的更多相关文章

  1. Springboot+RestTemplate 简单使用

        spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值 ...

  2. springboot+thymeleaf简单使用

    关于springboot想必很多人都在使用,由于公司项目一直使用的是SpringMVC,所以自己抽空体验了一下springboot的简单使用. 环境搭建 springbooot的环境搭建可以说很灵活, ...

  3. Springboot接口简单实现生成MySQL插入语句

    Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...

  4. SpringBoot 搭建简单聊天室

    SpringBoot 搭建简单聊天室(queue 点对点) 1.引用 SpringBoot 搭建 WebSocket 链接 https://www.cnblogs.com/yi1036943655/p ...

  5. SpringBoot 发送简单邮件

    使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...

  6. SpringBoot入门 简单搭建和使用

    前言 差不多两年前,那个时候我准备要做毕业设计了,才第一次知道java有框架这种东西,在网上找了好多SSM的教程,那会儿真的是Spring+SpringMVC+MyBatis搭建的,印象极深的是还要写 ...

  7. Element ui结合springboot的简单实战

    Eelment UI简单实战 前端开发 1 创建项目,导入element ui(略) 2 大致设计出想要的效果,如下 3 创建包 根据设计的大致模样在项目的components中创建对应的包,方便以后 ...

  8. Java Springboot webSocket简单实现,调接口推送消息到客户端socket

    Java Springboot webSocket简单实现,调接口推送消息到客户端socket 后台一般作为webSocket服务器,前台作为client.真实场景可能是后台程序在运行时(满足一定条件 ...

  9. spring-boot RestTemplate 连接池

    以前我们项目都是基于Apache HttpClient 连接池进行web 接口调用,后来用spring-boot, 发现 RestTemplate 挺好用. 简单介绍下: 什么是RestTemplat ...

随机推荐

  1. Java对象的强、软、弱和虚引用+ReferenceQueue

    Java对象的强.软.弱和虚引用+ReferenceQueue 一.强引用(StrongReference) 强引用是使用最普遍的引用.如果一个对象具有强引用,那垃圾回收器绝不会回收它.当内存空间不足 ...

  2. 新增的input

    原有的input类型: input标签原有的type类型: text(普通文本框,默认字) button(普通按钮) password(密码框)   submit(提交按钮) radio(单选框) r ...

  3. 【JavaScript 从零开始】 数字 文本 包装对象

    JavaScript中的算术运算 JavaScript 还自称更加复杂的算术运算,这些复杂的运算通过作为Math对象的属性定义的函数和常量来实现: Math.pow(2,53) //=>9007 ...

  4. weixin.com域名易主 传交易价格仅次360.com

    据业内人士透露,weixin.com双拼域名今日易主,交易价格在几千万级别,有传闻其交易价格仅次于360.com. 从whois信息查看可知,weixin.com域名信息今日发生变更,目前域名的持有者 ...

  5. UIWebView 展示GIF/image

    代码: [web loadData:self.gifDataArr[index] MIMEType:@"image/gif" textEncodingName:@"&qu ...

  6. NGINX防御CC攻击教程

    CC攻击即http flood,以攻击成本低(只需数台http代理服务器即可实现攻击).隐蔽性强(中小CC攻击一般不会造成网络瓶颈).难防御(与正常访问的请求很难区分开).威力强大(造成和DDOS流量 ...

  7. javascript学习笔记(二)

    二.DOM DOM是"Document Object Model"(文档对象模型)的首字母缩写,当创建了一个网页并把它加载到WEB浏览器 中时,DOM就在后台生成,它讲根据你编写的 ...

  8. mac 更新macOS Sierra 之后无法正常关机

    参考网址h:ttps://www.zhihu.com/question/50940249 这里就简单记录一下,因为网上写的仔细的比较少,我也写一份,希望病友们可以更方便的找到解决办法 其实就是mysq ...

  9. 通过读写文本文件小结“关于python处理中文编码的问题”

    一.引言 无论学习什么程序语言,字符串这种数据类型总是着有非常重要.然而最近在学习python这门语言,想要显示中文,总是出现各种乱码.于是在网上查了很多资料,各说纷纭,我也尝试了许多的方法,有时候可 ...

  10. 前端学习之HTML(1)

    HTML标签学习 2018-10-31 记录一下学习的网站 http://www.w3school.com.cn http://www.runoob.com/ <!DOCTYPE html> ...