SpringBoot RestTemplate 简单包装解析
RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类。
在SpringBoot中对这个类进行简单的包装,变成一个工具类来使用,这里用到的是getForEntity和postForEntity方法,具体包装的代码内容
如下:
packagecn.eangaie.demo.util;
importcom.alibaba.fastjson.JSONObject;
importorg.springframework.http.*;
importorg.springframework.stereotype.Component;
importorg.springframework.util.MultiValueMap;
importorg.springframework.web.client.RestTemplate;
importjava.util.Map;
/**
*@authorEangaie
*@date2018/10/120012下午14:53
*网络请求,RestTemplate工具类
*/
@Component
publicclassRestTemplateUtil{
privateRestTemplaterestTemplate;
/**
*发送GET请求
*@paramurl
*@paramparam
*@return
*/
publicStringGetData(Stringurl,Mapparam){
restTemplate=newRestTemplate();
//请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
HttpHeadersheaders=newHttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
returnrestTemplate.getForEntity(url,String.class,param).getBody();
}
/**
*发送POST-JSON请求
*@paramurl
*@paramparam
*@return
*/
publicStringPostJsonData(Stringurl,JSONObjectparam){
restTemplate=newRestTemplate();
HttpHeadersheaders=newHttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.add("Accept",MediaType.APPLICATION_JSON.toString());
HttpEntityrequestEntity=newHttpEntity(param,headers);
returnrestTemplate.postForEntity(url,param,String.class).getBody();
}
/**
*发送POST表单请求
*@paramurl
*@paramparam
*@return
*/
publicStringPostFormData(Stringurl,MultiValueMapparam){
restTemplate=newRestTemplate();
//请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
HttpHeadersheaders=newHttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
returnrestTemplate.postForEntity(url,param,String.class).getBody();
}
}
在控制类里面调用函数,看看效果
@GetMapping("selectUser")
publicResultselectUser(intid){
user=userService.selectById(id);
returnResultUtil.success(user,"查询成功");
}
@GetMapping("testGetData")
publicStringtestGetData(){
Stringurl="http://localhost:81/sample/GetData?msg={msg}&author={author}";
Mapparam=newHashMap<>();
param.put("msg","Hello");
param.put("author","彦杰");
returnrestTemplateUtil.GetData(url,param);
}
@GetMapping("testPostJsonData")
publicStringtestPostJsonData(){
Stringurl="http://localhost:81/sample/PostData";
JSONObjectjsonObject=newJSONObject();
jsonObject.put("msg","hello");
jsonObject.put("author","彦杰");
returnrestTemplateUtil.PostJsonData(url,jsonObject);
}
@GetMapping("testPostFormData")
publicStringtestPostFormData(){
Stringurl="http://localhost:81/sample/PostFormData";
MultiValueMapparam=newLinkedMultiValueMap<>();
param.add("msg","Hello");
param.add("author","彦杰");
returnrestTemplateUtil.PostFormData(url,param);
}
@GetMapping("GetData")
publicStringgetData(Stringmsg,Stringauthor){
returnmsg+""+author;
}
@PostMapping("PostData")
publicStringpostData(@RequestBodyJSONObjectjsonObject){
Stringmsg=jsonObject.getString("msg");
Stringauthor=jsonObject.getString("author");
returnmsg+""+author;
}
@PostMapping("PostFormData")
publicStringPostFormData(Stringmsg,Stringauthor){
returnmsg+""+author;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。