于是想到利用url直接post或者get方式,提交给其他系统去做处理.
//jsonp跨域时候使用的参数 private static final String CALLBACK = "callback" ; //通过actionName来判断对方要调用我们的什么接口 private static final String ACTION_NAME = "actionName" ; //接口调用完毕之后,返回给对方的参数名称 private static final String KEY_RESULT = "result" ; //接口调用完毕之后,返回给对方的参数名称,这个message只有出错的时候才会有值 private static final String KEY_MESSAGE = "message" ; //对方传过来的url的参数名称 private String requestData ; public String getRequestData() { return requestData; } public void setRequestData(String requestData) { this.requestData = requestData; } //成功调用接口后,返回 RESP_SUCCESS_JSON private static final JSONObject RESP_SUCCESS_JSON; static{ // 初始化固定的json结果 // success json JSONObject json = new JSONObject(); json.put(KEY_RESULT, SUCCESS); RESP_SUCCESS_JSON = json; } //调用接口失败后,返回的结果 private JSONObject getErrorJson(String msg){ JSONObject ret = new JSONObject(); ret.put(KEY_RESULT, ERROR); ret.put(KEY_MESSAGE, msg); return ret; } public String execute() throws Exception{ // 解析请求数据 JSONObject reqJson; String action = "" ; jsonp = this.getHttpServletRequest().getParameter(CALLBACK); try{ //取出对方传过来的参数值,一串json reqJson = JSONObject.fromObject(requestData); // 取出action action = StringUtils.trimToEmpty(reqJson.getString(ACTION_NAME)); }catch (Exception e) { outputResultJson(getErrorJson("解析json请求错!" + e.getMessage())); return null; } // 执行接口action JSONObject respJson = new JSONObject(); try{ logger.info("xx系统调用接口:" + action); logger.info("传入JSON:" + requestData); //根据不同的actionName 调用不同的处理方法 if(action.equalsIgnoreCase("UpdateApply")){ // 申请表数据修改 respJson = updateApply(reqJson.getJSONObject(KEY_DATA)); }else if(action.equalsIgnoreCase("RateNotice")){ // 合同费率通知 // TODO 优先级稍低 郝晶说先不做 respJson = rateNotice(reqJson.getJSONObject(KEY_DATA)); }else if(action.equalsIgnoreCase("PickReport")){ // 调阅审核报告 respJson = pickReport(reqJson.getJSONObject(KEY_DATA)); }else if(action.equalsIgnoreCase("AddBlackList")){ // 加入黑名单 respJson = addBlackList(reqJson.getJSONArray(KEY_DATA)); }else if(action.equalsIgnoreCase("updateClicStatus")){ // 放款,签订合同,等待放款 更新clic状态 respJson = updateClicStatus(reqJson.getJSONObject(KEY_DATA)); } }catch (Exception e) { //这里的异常可以处理的更细致一些 respJson = getErrorJson("错误!" + e.getMessage()); } outputResultJson(respJson); return null; } /** 这里是针对jsonp跨域进行的处理,如果对方传了跨域所需的参数CALLBACK过来,并且有值的话, 使用jsonp的形式给对方返回值,一般适用于对方采用ajax方法调用我们的接口 */ private void outputResultJson(JSONObject resultJson) throws IOException{ this.getHttpServletResponse().setCharacterEncoding("UTF-8"); if(StringUtils.trimToEmpty(jsonp).length() > 0) this.getHttpServletResponse().getWriter().write(jsonp+"("+resultJson.toString()+")"); else this.getHttpServletResponse().getWriter().write(resultJson.toString()); } private JSONObject updateClicStatus(JSONObject reqJsonData) { String bsTrasnportId = reqJsonData.getString("bsApplyId"); String status = reqJsonData.getString("status"); logger.info("进件号:"+bsTrasnportId+"状态码:"+status); Environment.getInstance().getService(TransportService.class).updateClicStatus(Long.parseLong(bsTrasnportId), status); return RESP_SUCCESS_JSON; }
使用过程中,大量的接口使用在ajax调用中,这时只能采用jsonp进行跨域.
而又有很多是直接在service中调用,这时候,就只能采用模拟提交表单方式了:
/** * 获得信审1.0接口地址连接 * @return */ private static HttpURLConnection getClic1InterfaceConn() throws Exception{ URL u = new URL(Property.getConstants("CHARGE_DETAIL")); HttpURLConnection conn = (HttpURLConnection)u.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setUseCaches(false); return conn; } public static void synLoanData(){ List<Map> mcsMap = new ArrayList<Map>(); for(MortgagorContact mc : mcs){ //把对象转换成key,value形式的map mcsMap.add(BeanUtils.describe(mc)); // 组织json JSONObject data = new JSONObject(); data.put("bsApplyId", String.valueOf(trans.getBsApplyId())); data.put("decision", BeanUtils.describe(bd)); // 决策信息 data.put("contact", mcsMap); // 取联系人 data.put("status", StringUtils.trimToEmpty(trans.getProcessNode())); // 进件状态 } // 提交 ,获得其他系统的接口链接 HttpURLConnection conn = getClic1InterfaceConn(); try{ log.info("同步数据JSON:" + data.toString()); OutputStream os = conn.getOutputStream(); //往接口中提交参数 IOUtils.write("act=borrow", os); IOUtils.write("&client=" + URLEncoder.encode(data.toString(), "utf-8"), os); os.flush(); os.close(); String response = IOUtils.toString(conn.getInputStream()); log.info("同步数据给信审1.0,[Response:" + response + "]"); }finally{ conn.disconnect(); } }
如果采用大量的不同的url去做处理,会造成大量的配置文件,造成管理上的混乱,所以采用一个url,根据参数的不同去调用不同的处理方式去解决.
//根据不同的actionName 调用不同的处理方法 根据参数 action的值,去判断调用哪个service方法.
这样就相当通用了.
:)