欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Springboot RestTemplate 简单使用解析

时间:2021-04-13 09:12:44|栏目:JAVA代码|点击:

前言

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;
}

上一篇:Java线程中start和run方法全面解析

栏    目:JAVA代码

下一篇:Java通过FTP服务器上传下载文件的方法

本文标题:Springboot RestTemplate 简单使用解析

本文地址:http://www.codeinn.net/misctech/100181.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有