如何在Springboot中使用RestTemplate-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

如何在Springboot中使用RestTemplate

这期内容当中小编将会给大家带来有关如何在Springboot中使用RestTemplate,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联是专业的安宁网站建设公司,安宁接单;提供网站制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行安宁网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

首先是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 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 requestEntity = new HttpEntity(param, headers);
		return restTemplate.postForEntity(url,param,String.class).getBody();
	}
	/**
* 发送POST 表单请求
* @param url
* @param param
* @return
*/
	public String PostFormData(String url,MultiValueMap 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 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 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 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 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
转载来于:http://kswsj.cn/article/geddsi.html

其他资讯