• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot RestTemplate 简单包装解析

springboot 搞代码 4年前 (2022-01-09) 29次浏览 已收录 0个评论

RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类。

在SpringBoot中对这个类进行简单的包装,变成一个工具类来使用,这里用到的是getForEntity和postForEntity方法,具体包装的代码内容

如下:

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.springfra<strong>本文来源gaodai#ma#com搞@代~码^网+</strong>mework.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();
	}
}

在控制类里面调用函数,看看效果

@GetMapping("selectUser")
public Result selectUser(int id){
	user=userService.selectById(id);
	return ResultUtil.success(user,"查询成功");
}
@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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot RestTemplate 简单包装解析
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址