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; } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM