标签:from str 网站 color post www obj div 传递
可以采用RestTemplate方式发起Rest Http调用,提供有get、post等方式。
此处使用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"); } ? }
工具类中提供了远程过程中传递参数的两种格式:
其中 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); }
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); }
关于RestTemplate还有很多可调用的API,可以查看官方网站了解
http://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate
标签:from str 网站 color post www obj div 传递
原文地址:https://www.cnblogs.com/nangonghui/p/10180815.html