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. 国际化实现之安装脚手架vue以及打包问题

    做这个项目用的是vue+element UI来实现的响应式布局,现主要说一下国际化这块的实现. 第一步:新建文件夹i18n 第二步:配置cn.js.en.js等文件内容 cn.js import en ...

  2. python队列、线程、进程、协程

    目录: 一.queue 二.线程 基本使用 线程锁 自定义线程池 生产者消费者模型(队列) 三.进程 基本使用 进程锁 进程数据共享 默认数据不共享 queues array Manager.dict ...

  3. mvc手把手教你写excel导入

    实习狗的每天新知识日常 准备工作: 1.在项目中添加对NPOI的引用,NPOI下载地址:http://npoi.codeplex.com/releases/view/38113 2.NPOI学习系列教 ...

  4. CSS 通过使用Important覆盖所有其他样式

    在许多情况下,您将使用CSS库.这些可能会意外覆盖您自己的CSS.所以当你绝对需要确定一个元素具有特定的CSS时,可以使用 !important. 让我们回到之前的 pink-text class 声 ...

  5. (一)面向对象的javascript

    javascript是一门典型的动态类语言 一:鸭式辨型(指导我们关注对象的行为,而不关注对象本身). var duck = { duckString: function(){ console.log ...

  6. 用于深拷贝的扩展方法 C#

    using System.Runtime.Serialization.Formatters.Binary; using System.IO; public static class Tool { pu ...

  7. jquery之Ajax(一)

    1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (String) : 请求的HTML页的URL地址. data (M ...

  8. CSS流体(自适应)布局下宽度分离原则——张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1463 一.简短的前言 ...

  9. c#如何将子窗体显示到父窗体的容器(panel)控件中

    如何将一个窗体显示到一个容器控件中,刚开始想的比较简单,用窗体容器控件添加一般控件的方法,试了一试,代码如下: Form2 frm = new Form2(); this.panel1.Control ...

  10. 【代码笔记】iOS-左右可滑动的选择条

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...