SpringCloud(11)—— 项目经验之(RestTemplate请求)

http://www.cnblogs.com/BensonHe/p/4602746.html



/**
* 对接国寿i购系统 * * @param query */ @Override public ResponseInfo<Map<String, String>> dockIGou(ExclusiveDockIGouInputDTO query) { try { //请求格式(定位具体方法) Map map2 = new HashMap(); map2.put("state", "new"); map2.put("action", "ShopAction"); map2.put("method", "mall"); //请求格式(具体参数) Map map3 = new HashMap(); map3.put("Usercode", query.getUsercode()); map3.put("ExtraParams", query.getExtraparams()); map3.put("Mobile", query.getMobile()); map3.put("Source", configProperties.getIgousouce()); //秘钥 Map mp4 = new HashMap(); mp4.put("id", ""); mp4.put("Resolution", ""); mp4.put("model", "iPhone"); mp4.put("enterprise", "N"); mp4.put("appversion", "2.0"); mp4.put("session", ""); mp4.put("buildversion", "Ver:1.5_CUS_PROD_131212"); mp4.put("Memory", ""); mp4.put("password", ""); mp4.put("sysversion", "9.2"); mp4.put("manufacturer", "Apple"); mp4.put("devicemodel", "iPhone"); mp4.put("name", ""); mp4.put("udid", "DA3A075D-CE35-47B7-89DB-8ECB9013D969"); //请求格式 Map map1 = new HashMap(); map1.put("head", map2); map1.put("info", map3); map1.put("security", mp4); LOGGER.debug("获取i购地址接口参数:" + JSONObject.toJSONString(map1)); String html = sendToService(configProperties.getIgouurl(), JSONObject.toJSONString(map1)); LOGGER.debug("获取掌上国寿对接i购地址:" + JSONObject.parseObject(html)); return new ResponseInfo(true, "success", JSONObject.parseObject(html)); } catch (Exception e) { LOGGER.error("对接国寿i购异常,原因:" + CommonUtil.getExceptionStackTrace(e)); return new ResponseInfo<>(false, "系统异常...", 400); } } /** * 发生post请求 * * @param serverUrl * @param jsonObj * @return */ public static String sendToService(String serverUrl, String jsonObj) { String result = ""; try { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj, headers); result = restTemplate.postForObject(serverUrl, formEntity, String.class); LOGGER.debug("连接服务器:" + serverUrl + ",返回结果" + result); return result; } catch (Exception e) { LOGGER.error("连接服务器:" + serverUrl + "失败,原因" + e.getMessage()); return result; } }

 

posted @ 2017-07-07 13:35  xu_shuyi  阅读(1402)  评论(0编辑  收藏  举报