接口项目servlet的一种处理方式,将异常返回给调用者【我】
2023-09-27 14:28:22 时间
接口项目servlet的一种处理方式,其他层有异常全部网上抛,抛到servlet层,将异常返回给调用者:
Servlet层:
private void processRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException { InputStream inStream =null; BufferedReader br =null; String reJson=""; String rtJson=""; String tranId=""; //返回报文字段 String resultCode = "1"; String resultMsg = "失败"; try { httpRequest.setCharacterEncoding("UTF-8"); inStream = httpRequest.getInputStream(); String reqUrl= httpRequest.getRequestURI(); syslogger.info(reqUrl); String[] method = reqUrl.split("/", 0); Map headMap = this.getHeaderMap(httpRequest); Map params = this.getParameterMap(httpRequest); syslogger.info(params); br = new BufferedReader(new InputStreamReader(inStream,"UTF-8")); String str=br.readLine(); StringBuffer json = new StringBuffer(); while (str != null) { try { json.append(str); str = br.readLine(); } catch (Exception blExc) { System.out.println("请求参数获取出错------------str:"+str); throw new CommonRunException(blExc.getMessage(), blExc, ""); } } reJson=json.toString(); syslogger.info("resultlog 请求------------"+json); // String httpMethod = httpRequest.getMethod().toUpperCase(); System.out.println("resultlog 请求------------"+json); //调用相关的程序逻辑 tranId=RequestUtil.getMapValue(headMap,"X-CTG-Request-Id"); if("".equals(tranId)){ tranId=RequestUtil.getMapValue(headMap,"X-CTG-Request-ID"); } //调用实现类参数 String param = ""; try { //逐层解析报文获取请求参数 JSONObject jsobRst1 = JSONObject.fromObject(json.toString()); syslogger.info("jsobRst1 请求------------"+jsobRst1); // String svcCont = jsobRst1.getString("svcCont"); // JSONObject requestObject = JSONObject.fromObject(svcCont); // param = requestObject.get("requestObject").toString(); param = jsobRst1.toString(); } catch (Exception e) { System.out.println("请求参数解析出错------------str:"+json); syslogger.info("解析报文出错 请求------------"+e); throw new Exception("解析报文出错 请求------------"+e); } //逻辑处理类 参考 GETcustCRMProCustationGetImagesAPI GETcustSyncSaleAPI logic = new GETcustSyncSaleAPI(); //绑定参数 logic.injectDepObject(param); //实现类执行请求 logic.process(); //调用实现类返回值处理 Map implRtn = new HashMap(); implRtn = (Map)logic.getReturnObject(); if (implRtn.containsKey("error")) { resultCode = "1"; resultMsg = implRtn.get("error").toString(); }else { resultCode = "0"; if (implRtn.containsKey("msg")) { resultMsg = implRtn.get("msg").toString(); }else { resultMsg = "成功"; } } httpResponse.setCharacterEncoding("UTF-8"); httpResponse.setHeader("Content-type", "application/json;charset=UTF-8"); //返回接口调用的处理结果 OutputStreamWriter objOutputStrm = new OutputStreamWriter(httpResponse.getOutputStream(),"UTF-8"); try { // Map respMap = new HashMap(); JSONObject SvcCont = new JSONObject(); SvcCont.put("resultCode", resultCode); SvcCont.put("resultMsg", resultMsg); // respMap.put("svcCont", SvcCont); // JSONObject resObject = JSONObject.fromObject(respMap); // syslogger.info("resultlog 返回------------"+respMap); // objOutputStrm.write(resObject.toString()); // rtJson=resObject.toString(); objOutputStrm.write(SvcCont.toString()); rtJson=SvcCont.toString(); } catch (Exception e) { e.printStackTrace(); }finally { objOutputStrm.flush(); objOutputStrm.close(); } } catch (Exception e) { syslogger.info(e.toString()); syslogger.fatal(e.getMessage(), e); throw new ServletException(e.getMessage()); }finally{ try { if (br != null){ br.close(); br = null; } if (inStream != null){ inStream.close(); inStream = null; } } catch (IOException e) { e.printStackTrace(); }finally{ // setInsertSoapMsgHistory( reJson, rtJson, // "0", tranId,"CustSyncSale"); } } }
service层:
public void processLogic() { //返回map respMap = new HashMap(); syslogger.info("processLogic start"); syslogger.info("paraMap start"+respMap); eimServiceHolder = DBServiceHolderFactory.getDBServiceHolder(IDBServiceHolderConsts.JTCRM_APP); //返回map JSONObject SvcCont = new JSONObject(); Map<Object, Object> map = new HashMap<Object, Object>(); String statusCD = null; //接口标识 String type = ""; Map list = new HashMap(); JSONObject jsonObject = null; if(paraMap == null || paraMap.equals("")){ syslogger.info("GETcustxxxx.processLogic............................请求报文接受异常!"); return; }else{ jsonObject = JSONObject.fromObject(paraMap); map = (Map)jsonObject; } System.out.println("入参:"+paraMap); String result = paraMap; statusCD = CSUtil.o2s(map.get("statusCD")); try { if("1200".equals(statusCD) || "1000".equals(statusCD)){ //新建送审/审核通过 Object rtn = eimServiceHolder.execute(service, "insertRel", new Object[]{result}); respMap.put("msg", rtn.toString()); }else if ("1100".equals(statusCD)) {//审核不通过/删除 Object rtn = eimServiceHolder.execute(service, "deleteRel", new Object[]{result}); respMap.put("msg", rtn.toString()); System.out.println("删除(更新)成功"); }else { System.out.println("入参中statusCD异常,statusCD:"+statusCD); throw new Exception("入参中statusCD异常,statusCD:"+statusCD); } syslogger.info("respMap start"+respMap); } catch (Exception e) { e.printStackTrace(); respMap.put("error", e.toString()); } syslogger.info("processLogic end"); return; }
相关文章
- 利用python调用halcon接口(halcon12.0版本)【源码篇幅少,直接在文章中附源码】
- ThinkPhp3.2.3 多项目 后台 APP接口设计 框架设计
- 05计算机网络协议、接口、服务的概念
- 如何设计一个安全的对外接口?
- 基于 python 的接口自动化测试项目,完整实战项目附源码分享
- 2022最全面的教程,RF接口自动化框架项目实战
- 经常被后端开发问起的接口测试,月薪20K也搞不懂这个?
- Postman接口测试 —— 使用postman运行接口
- java调用webservice接口 几种方法
- 《Java语言导学(原书第6版)》一2.4 接口
- Java多线程-通过实现 Runnable 接口来创建线程
- 易宝支付Demo,生产中封装成简洁的代付接口,不用request如何获取项目运行时的真实路径(转)
- 使用C++的接口
- 《Springboot极简教程》SpringBoot plus Mongodb开发企业级restfeel接口测试平台
- 拿到一个新项目,如何开展接口自动化?
- postman 做接口测试
- 使用CXF+spring创建一个web的接口项目
- ant+Jacoco 统计tomcat远程部署后项目接口自动化测试或者功能测试代码覆盖率
- 【树莓派4B深度学习 垃圾分类】Chap.3 树莓派安装opencv并测试视频接口实时视频流的垃圾分类【深度学习 招式篇】
- postman请求依赖登录的接口
- eggjs 怎么实现账单详情页的获取详情接口?
- 客快物流大数据项目(一百零七):物流信息查询服务接口开发解决方案