这篇文章将为大家详细讲解有关SpringBoot RestTemplate简单包装的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
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.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();
}
}
在控制类里面调用函数,看看效果
@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;
}
关于“SpringBoot RestTemplate简单包装的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。