Springboot RestTemplate 简单使用解析

 更新时间:2019年08月08日 10:05:41   作者:谢彦杰  
这篇文章主要介绍了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 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使用pdfbox提取PDF图片的代码示例

    Springboot使用pdfbox提取PDF图片的代码示例

    PDFBox是一个用于创建和处理PDF文档的Java库,它可以使用Java代码创建、读取、修改和提取PDF文档中的内容,本文就给大家介绍Springboot如何使用pdfbox提取PDF图片,感兴趣的同学可以借鉴参考
    2023-06-06
  • 一文解决pom.xml报错Dependency "xxx" not found的问题

    一文解决pom.xml报错Dependency "xxx" not f

    我们在使用maven进行jar包管理时有时会遇到pom.xml中报错Dependency “XXX” not found,所以在本文中将给大家介绍一下pom.xml报错Dependency "xxx" not found的解决方案,需要的朋友可以参考下
    2024-01-01
  • Java中控制多线程执行顺序的8种方法

    Java中控制多线程执行顺序的8种方法

    在并发编程中,控制线程执行顺序是一个常见且重要的需求,Java提供了多种机制来实现线程顺序控制,本文将全面介绍8种核心方法,涵盖从基础到高级的各种场景需求,需要的朋友可以参考下
    2025-03-03
  • 一文彻底搞定Java哈希表和哈希冲突

    一文彻底搞定Java哈希表和哈希冲突

    本文介绍了什么是哈希表?什么是哈希函数?什么是哈希冲突?三个问题的解决方案,文中有非常详细的代码示例,对正在学习java的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05
  • Java8新特性之Base64详解_动力节点Java学院整理

    Java8新特性之Base64详解_动力节点Java学院整理

    这篇文章主要为大家详细介绍了Java8新特性之Base64的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Java将json对象转换为map键值对案例详解

    Java将json对象转换为map键值对案例详解

    这篇文章主要介绍了Java将json对象转换为map键值对案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • 一文带你搞懂Java中Synchronized和Lock的原理与使用

    一文带你搞懂Java中Synchronized和Lock的原理与使用

    这篇文章主要为大家详细介绍了Java中Synchronized和Lock的原理与使用,文中的示例代码讲解详细,对我们学习Java有一定的帮助,需要的可以参考一下
    2023-04-04
  • java常用工具类 Reflect反射工具类、String字符串工具类

    java常用工具类 Reflect反射工具类、String字符串工具类

    这篇文章主要为大家详细介绍了java常用工具类,包括Reflect反射工具类、String字符串工具类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • Java中BigDecimal类的add()的使用详解

    Java中BigDecimal类的add()的使用详解

    这篇文章主要介绍了Java中BigDecimal类的add()的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • 移动开发Spring Boot外置tomcat教程及解决方法

    移动开发Spring Boot外置tomcat教程及解决方法

    这篇文章主要介绍了移动开发SpringBoot外置tomcat教程,需要的朋友可以参考下
    2017-11-11

最新评论