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
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();

 

}

}

 

    这里编写了三个函数,一个是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 简单使用的更多相关文章

  1. SpringBoot+RestTemplate 简单包装

        RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类.     在SpringBoot中对这个类进行 ...

  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. haproxy配置文件详解--转

    原始出处:http://itnihao.blog.51cto.com/1741976/915537 #/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.c ...

  2. rabbitmq不同模式的交换机使用

    交换机的功能主要是接收消息并且转发到绑定的队列,交换机不存储消息,在启用ack模式后,交换机找不到队列会返回错误.交换机有四种类型:Direct, topic, Headers and Fanout( ...

  3. “Device eth0 does not seem to be present”解决办法

    在KVM中克隆出新的CentOS虚拟机时,出现如下问题:# service network restartShutting down loopback insterface: [ OK ]Bringi ...

  4. automapper 自动映射 集成asp.net Core2.1

    学习博文:https://www.cnblogs.com/yan7/p/8085410.html 1.使用nuget 安装 <PackageReference Include="Aut ...

  5. 第三节:我的第一个Java程序

    一.编写我的第一个Java程序流程: 1.新建一个文本文档:在电脑任意位置“右击”----->选择“新建”----->选择“文本文档”: 2.修改文档名与后缀名:“右击”新建的文本文档-- ...

  6. SpringMVC整合Shiro安全框架(一)

    一. 准备工作 1. 本文参考自张开涛的 <跟我学Shiro> 二. 简介 1. Apache Shiro是Java的一个安全框架.可以帮助我们完成:认证.授权.加密.会话管理.与Web集 ...

  7. 解决:jsp 页面不全,response 内容不完整

    前言:今天 jsp 页面输出不完整这个问题困扰了我几个小时,终于发现问题并解决了. 环境: tomcat 8.0.17 x64 jsp springmvc vue 问题: 本来页面正常,但加了几行代码 ...

  8. POJ3278(KB1-C 简单搜索)

    Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and wants ...

  9. Vue双向绑定原理详解

    前言:Vue最核心的功能之一就是响应式的数据绑定模式,即view与model任意一方改变都会同步到另一方,而不需要手动进行DOM操作,本文主要探究此功能背后的原理. 思路分析 以下是一个最简单的双向绑 ...

  10. 【canvas系列】canvas实现“ 简单的Amaziograph效果”--画对称图【强迫症福利】

    标题很难引人入胜,先放个效果图好了 如果图片吸引不了你,那我觉得也就没啥看的了. demo链接: https://win7killer.github.io/demo_set/html_demo/can ...