接口项目servlet的一种处理方式,将异常返回给调用者我

Posted libin6505

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口项目servlet的一种处理方式,将异常返回给调用者我相关的知识,希望对你有一定的参考价值。

 

 

接口项目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;
    }

 

以上是关于接口项目servlet的一种处理方式,将异常返回给调用者我的主要内容,如果未能解决你的问题,请参考以下文章

2018-2019-1 20165337 《信息安全系统设计基础》第七周学习总结

WCF 异常处理

Servlet 与 CGI 的比较

20165232 第七周学习总结

JAVA后台 关于如何从后台传递信息在jsp前端

SpringBoot全局异常处理与定制404页面