SpringBoot关于系统之间的远程互相调用

1、SpringBoot关于系统之间的远程互相调用

可以采用RestTemplate方式发起Rest Http调用,提供有get、post等方式。

1、1远程工具类

此处使用Post方式,参考下面封装的HttpClient类 1.1

/**
 * Created by @kai on 2018/12/24/024.
 * Time: 13:54
 * Desc:    远程连接工具类
 */
@Service
public class HttpClient {
​
   /**
    * 根据远程地址发起访问-参数类型为form表单
    * @param url   远程地址
    * @param method    远程方法
    * @param params     方法参数
    * @return
    */
   public Object client(String url,HttpMethod method,MultiValueMap<String,String> params){
      RestTemplate restTemplate = new RestTemplate();
      HttpHeaders headers = new HttpHeaders();
      headers.add("Content-Type", "application/x-www-form-urlencoded");
      HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, headers);
      ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,httpEntity,String.class);
      String body = responseEntity.getBody();
      JSONObject jsonObject = JSONObject.parseObject(body);
      return jsonObject.get("data");
   }
​
   /**
    * 根据远程地址发起访问-参数类型为JSON
    * @param url   远程地址
    * @param method    远程方法
    * @param params     方法参数
    * @return
    */
   public Object clientJson(String url,HttpMethod method,Map<String,Object> params){
      RestTemplate restTemplate = new RestTemplate();
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
      cn.hutool.json.JSONObject jsonObject = JSONUtil.parseFromMap(params);
      HttpEntity<cn.hutool.json.JSONObject> httpEntity = new HttpEntity<>(jsonObject, headers);
      ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,httpEntity,String.class);
      String body = responseEntity.getBody();
      JSONObject jsonObjectResult = JSONObject.parseObject(body);
      return jsonObjectResult.get("data");
   }
​
}
[1.1​]  

1、2远程参数说明

工具类中提供了远程过程中传递参数的两种格式:

其中 headers.add("Content-Type", "application/x-www-form-urlencoded") 为form表单格式,支持键值对数据传输;

当参数类型为form表单时,数据需要封装成MultiValueMap<String,String>格式,前台使用controller接受时,可以直接使用 MultiValueMap 变量接收,参照代码如下 1.2

/**
 * 保存分组策略对象
 * @param
 * @return
 */
@RequestMapping(value = "/saveDocGroupPolicy",method = RequestMethod.POST)
public ApiResult saveGroupPolicy(@RequestParam MultiValueMap<String,String> paramMap,@Valid GroupStrategyIO groupStrategyIO){
   Integer userId = ShiroUtil.getExamUserId();
   List<String> userList = new ArrayList<>();
   userList.add(userId+"");
   paramMap.put("userId",userList);
   Object jsonObject = httpClient.client(ExamConfigConstants.url+"/exam/configPolicy/saveDocGroupPolicy", HttpMethod.POST, paramMap);
   return ApiResult.success(jsonObject);
}
[1.2]  接受参数为form对象

headers.setContentType(MediaType.APPLICATION_JSON_UTF8) 为json数据格式

当参数为json格式时,远程服务器接受参数需加上注解@RequestBody,对于复杂参数可以使用对象接受,将对象转为Map,对数据进行加工,再将map转化为JSONObject,参照代码如下:1.3

/**
 * 保存试卷策略
 * @param paperStrategyIO   试卷策略对象
 * @return
 */
@RequestMapping(value = "/savePaperConfig")
public ApiResult savePaperConfig(@RequestBody PaperStrategyIO paperStrategyIO){
   Map<String, Object> paramMap = BeanUtil.beanToMap(paperStrategyIO);
   Integer userId = ShiroUtil.getExamUserId();
   paramMap.put("userId",userId);
   Object jsonObject = httpClient.clientJson(ExamConfigConstants.url+"/exam/paper/savePaperConfigWithMap", HttpMethod.POST, paramMap);
   return ApiResult.success(jsonObject);
}
[1.3​]  接收参数为复杂json串

2、后记

关于RestTemplate还有很多可调用的API,可以查看官方网站了解

http://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate

3、写在最后

如果,你感觉此篇博客有用,请评论一个1,若感觉鸡肋,可以评论一个0,我会继续努力完善!!!

原文地址:https://www.cnblogs.com/nangonghui/p/10180815.html