//String 转化为JSON对象
JSONObject jsonObject = JSONObject.parseObject(data);
//JSON对象转化为Json 字符串
paramMap.put("bookingRule",bookingRule.toJSONString());
案例:将数据进行封装,并通过http发起跳转
//封转数据
public boolean saveHospital(String data) {
JSONObject jsonObject = JSONObject.parseObject(data);
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("hoscode",this.getHoscode());
paramMap.put("hosname",jsonObject.getString("hosname"));
paramMap.put("hostype",jsonObject.getString("hostype"));
paramMap.put("provinceCode",jsonObject.getString("provinceCode"));
paramMap.put("cityCode", jsonObject.getString("cityCode"));
paramMap.put("districtCode",jsonObject.getString("districtCode"));
paramMap.put("address",jsonObject.getString("address"));
paramMap.put("intro",jsonObject.getString("intro"));
paramMap.put("route",jsonObject.getString("route"));
//图片
paramMap.put("logoData", jsonObject.getString("logoData"));
JSONObject bookingRule = jsonObject.getJSONObject("bookingRule");
paramMap.put("bookingRule",bookingRule.toJSONString());
paramMap.put("timestamp", HttpRequestHelper.getTimestamp());
paramMap.put("sign", MD5.encrypt(this.getSignKey()));
JSONObject respone =
HttpRequestHelper.sendRequest(paramMap,this.getApiUrl()+"/api/hosp/saveHospital");
System.out.println(respone.toJSONString());
if(null != respone && 200 == respone.getIntValue("code")) {
return true;
} else {
throw new YyghException(respone.getString("message"), 201);
}
}
/**
* 封装同步请求
* @param paramMap
* @param url
* @return
*/
public static JSONObject sendRequest(Map<String, Object> paramMap, String url){
String result = "";
try {
//封装post参数
StringBuilder postdata = new StringBuilder();
for (Map.Entry<String, Object> param : paramMap.entrySet()) {
postdata.append(param.getKey()).append("=")
.append(param.getValue()).append("&");
}
log.info(String.format("--> 发送请求:post data %1s", postdata));
byte[] reqData = postdata.toString().getBytes("utf-8");
byte[] respdata = HttpUtil.doPost(url,reqData);
result = new String(respdata);
log.info(String.format("--> 应答结果:result data %1s", result));
} catch (Exception ex) {
ex.printStackTrace();
}
return JSONObject.parseObject(result);
}
/**
* post 方式发送http请求.
*
* @param strUrl
* @param reqData
* @return
*/
public static byte[] doPost(String strUrl, byte[] reqData) {
return send(strUrl, POST, reqData);
}
/**
* @param strUrl
* @param reqmethod
* @param reqData
* @return
*/
public static byte[] send(String strUrl, String reqmethod, byte[] reqData) {
try {
URL url = new URL(strUrl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.setDoOutput(true);
httpcon.setDoInput(true);
httpcon.setUseCaches(false);
httpcon.setInstanceFollowRedirects(true);
httpcon.setConnectTimeout(CONN_TIMEOUT);
httpcon.setReadTimeout(READ_TIMEOUT);
httpcon.setRequestMethod(reqmethod);
httpcon.connect();
if (reqmethod.equalsIgnoreCase(POST)) {
OutputStream os = httpcon.getOutputStream();
os.write(reqData);
os.flush();
os.close();
}
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream(),"utf-8"));
String inputLine;
StringBuilder bankXmlBuffer = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
bankXmlBuffer.append(inputLine);
}
in.close();
httpcon.disconnect();
return bankXmlBuffer.toString().getBytes();
} catch (Exception ex) {
log.error(ex.toString(), ex);
return null;
}
}
?
//上传医院接口
@PostMapping("saveHospital")
public Result saveHosp(HttpServletRequest request) {
//获取传递过来医院信息
Map<String, String[]> requestMap = request.getParameterMap();
Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
//1 获取医院系统传递过来的签名,签名进行MD5加密
String hospSign = (String)paramMap.get("sign");
//2 根据传递过来医院编码,查询数据库,查询签名
String hoscode = (String)paramMap.get("hoscode");
String signKey = hospitalSetService.getSignKey(hoscode);
//3 把数据库查询签名进行MD5加密
String signKeyMd5 = MD5.encrypt(signKey);
//4 判断签名是否一致
if(!hospSign.equals(signKeyMd5)) {
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
//传输过程中“+”转换为了“ ”,因此我们要转换回来
String logoData = (String)paramMap.get("logoData");
logoData = logoData.replaceAll(" ","+");
paramMap.put("logoData",logoData);
//调用service的方法
hospitalService.save(paramMap);
return Result.ok();
}
/**
*
* @param paramMap
* @return
*/
public static Map<String, Object> switchMap(Map<String, String[]> paramMap) {
Map<String, Object> resultMap = new HashMap<>();
for (Map.Entry<String, String[]> param : paramMap.entrySet()) {
resultMap.put(param.getKey(), param.getValue()[0]);
}
return resultMap;
}
|