Springboot+RestTemplate 简单使用
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
private RestTemplate restTemplate;
/** * 发送GET请求 * @param url * @param param * @return */ public String GetData(String url, Map<String,String> param){ restTemplate=new RestTemplate(); // 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交 HttpHeaders headers = 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 = headers.setContentType(MediaType.APPLICATION_JSON_UTF8); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<JSONObject> requestEntity = 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 = 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 简单使用的更多相关文章
- SpringBoot+RestTemplate 简单包装
RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类. 在SpringBoot中对这个类进行 ...
- 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 ...
随机推荐
- Math.floor,Math.ceil,Math.rint,Math.round用法
一.Math.floor函数讲解 floor原意:地板.Math.floor函数是求一个浮点数的地板,就是求一个最接近它的整数,它的值小于或等于这个浮点数.看下面的例子: package com.qi ...
- 【LeetCode题解】231_2的幂(Power-of-Two)
目录 描述 解法 1:判断整数 \(x\) 的二进制表示中是否只有一位为1 实现方式 1:除以 2 Java 实现(非递归) Python 实现(非递归) Java 实现(递归) Python 实现( ...
- Android Studio 打包生成 APK
1. 第一步 Build -> Generate Signed APK 2. 之后会要求开发者输入相关的密钥文件和密码 如果有则找到对应的 .jks 文件输入密码完成相应操作,否则则创建一个对应 ...
- MVC页面缓存
1.OutputCache 属性 contact.cshtml [OutputCache(Duration=10)] public ActionResult Contact() { ...
- SEDA架构程序实现
一.SEDA SEDA全称是:stage event driver architecture,中文直译为“分阶段的事件驱动架构”,它旨在结合事件驱动和多线程模式两者的优点,从而做到易扩展,解耦合,高并 ...
- 【原】Spring activiti 环境搭建之数据库创建
由于在开发工作流的时候,避免不了要保存一些数据和流程走向;所以在搭建Spring activiti开发环境的时候需要把官方提供的23张表创建到我们的DB,后续的流程都会在这些表中记录. 1.创建代码如 ...
- JVM读书笔记
1 概念 java virtual machine为java虚拟机,运行使用jdk中编译器编译的java程序. 2 JVM内存模型 程序计数器:线程私有.当前线程正在执行的行号指示器. Java虚拟机 ...
- Form表单和里边的小部件
一.Form表单:form表单是用来收集用户信息,并向后台提交信息的区域表单: 1.属性 “action” 是 “行为“的意思,该属性的值表示:用户提交信息到哪个页面: 2.属性”method“ 是” ...
- WebApiTestHelpPage
这是个什么鬼,第一次见到的时候,我也不知道就花几天时间看了下它的代码 在网上搜索WebApiTestHelpPage会出来很多相关页面 但是它们都是介绍怎么用的,要么就是怎么添加注释 它是怎么 ...
- @ConfigurationProperties与@value区别
@ConfigurationProperties与@value区别 @ConfigurationProperties @value 功能 批量注入配置文件中的属性 一个个指定 松散绑定 支持 不支 ...