惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
IT之家
IT之家
Last Week in AI
Last Week in AI
J
Java Code Geeks
L
LangChain Blog
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
博客园 - Franky
博客园 - 司徒正美
月光博客
月光博客
博客园 - 叶小钗
Vercel News
Vercel News
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
B
Blog RSS Feed
人人都是产品经理
人人都是产品经理
H
Help Net Security
G
Google Developers Blog
D
DataBreaches.Net

博客园 - hehoge

mysql mybatis 日期查询 druid报错:javax.management.InstanceNotFoundException: com.alibaba.druid:type=DruidDataSourceStat linux下安装Mysql mysql删除固定前缀的表 Mac上操作 androidStudio Unable to find a @SpringBootConfiguration xstream.fromXML 忽略没有的节点 Fatal error compiling: 无效的目标发行版: 1.8 NoSuchMethodError 一般是jar包冲突了 联通网络环境上无法访问http://repo1.maven.org/maven2/中央库解决,镜像库添加 如何在类中获取request,和网站路径 maven build 报release 400错误 mysql启动问题access denied for user 'root'@'localhost'(using password:YES) eclipse项目报红解决 Location Type Project 'testma' is missing required source folder: 'src/test/resources' testma Build 当遇到eclipse调试断点乱走数据不准确的时候,请maven clean,maven install git学习(一)与eclipse结合 反欺诈组之前接口项目学习 null值插入数据库会报错
adapter结构异常记录
hehoge · 2017-05-30 · via 博客园 - hehoge

Posted on 2017-05-30 16:36  hehoge  阅读(316)  评论()    收藏  举报

adapter结构异常记录,记录在这个类里,记录数据日志,在148行:

com.creditharmony.adapter.core.service.GeneralHttpService

package com.creditharmony.adapter.core.service;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.creditharmony.apporveadapter.bean.BaseInfo;
import com.creditharmony.apporveadapter.bean.BaseOutInfo;
import com.creditharmony.apporveadapter.bean.GeneralHttpInfoModel;
import com.creditharmony.apporveadapter.bean.GeneralReturnInfo;
import com.creditharmony.adapter.constant.Constant;
import com.creditharmony.apporveadapter.constant.ErrorType;
import com.creditharmony.adapter.constant.MsgKey;
import com.creditharmony.apporveadapter.exception.AdapterException;
import com.creditharmony.adapter.utils.AdapterUtils;
import com.creditharmony.adapter.utils.Messages;
import com.creditharmony.common.util.PropertyUtil;
import com.creditharmony.common.util.SpringContextHolder;

/**
 * @Class Name GeneralHttpService
 * @author yourname
 * @Create In 2016年12月3日
 */
@Controller
public class GeneralHttpService {
    /** 业务挡板测试区分名. */
    private static final String BAFFLE_FIX = "_Baffle";
    
    /** 日志. */
    private static final Logger logger = Logger.getLogger(GeneralHttpService.class);
    
    /** 属性文件. */
    static Properties properties = PropertyUtil.getProperties(Constant.CONFIG_PROPERTY);
    
    /** HttpServletRequest. */
    @Autowired
    private HttpServletRequest request;
    
    /** 参数记录处理. */
    @Autowired
    private IParamRecord paramRecord;
    
    /**
     * Client端调用统一接口.
     * 实现分发器效果
     * 
     * @param paramObj 调用参数
     * @return 返回参数
     */
    @ResponseBody
    @RequestMapping(value = "http/generalHttpService", method = RequestMethod.POST)
    public String exec(@RequestParam("content") String content) {
        
        // 取得唯一序列号
        String serialNum = AdapterUtils.getSerialNum();
        // 获得调用客户端IP地址
        String clientIp = getClientIP(request, serialNum);
        // 大金融的报文转为Model对象
        GeneralHttpInfoModel paramObj = JSON.parseObject(content, GeneralHttpInfoModel.class);

        logger.info(Messages.get(MsgKey.GENERALSERVICE_INPARAM, new String[] { content }));
        // 参数日志记录: 传入参数
        paramRecord.doInParamRecord(
                serialNum,
                content,
                clientIp,
                paramObj.getServiceName());

        // 取得业务参数Json
        String paramStr = paramObj.getParam();

        logger.info(Messages.get(MsgKey.GENERALSERVICE_INPARAM, new String[] { paramStr }));
        // 参数日志记录: 传入参数
        paramRecord.doInParamRecord(
                serialNum,
                paramStr,
                clientIp,
                paramObj.getServiceName());
        
        /*
         * 处理:取得业务实现类
         * 利用传入的serviceName,通过反射方式取得该业务实际类
         */
        IBaseService baseService = this.initService(paramObj.getServiceName());

        /*
         * 处理:调用实际业务处理
         * 通过父类抽象方法调用实现子类具体业务调用
         */
        GeneralReturnInfo out = new GeneralReturnInfo();
        BaseOutInfo outParam = null;
        // 返回对象参数
        String outParamStr = "";
        try {
            
            // 将传入的JSON报文转为Bean对象
            BaseInfo inBean = (BaseInfo) JSON.parseObject(paramStr, doCreatInObject(paramObj.getInClassName()).getClass());
            inBean.setSerialNum(serialNum);
            outParam = baseService.exec(inBean);
            out.setOutParam(JSON.toJSONString(outParam, SerializerFeature.WriteMapNullValue));

            outParamStr = JSON.toJSONString(out, SerializerFeature.WriteMapNullValue);
            
            /*
             * 处理:返回参数记录处理.
             */
            paramRecord.doOutParamRecord(
                    serialNum,
                    outParamStr);
            logger.info(Messages.get(MsgKey.GENERALSERVICE_OUTPARAM, new String[] { outParamStr }));
        } catch (Exception e) {
            boolean isNewExeption = e instanceof AdapterException;
            
            AdapterException businessException = null;
            // 将新产生的例外封装
            if (isNewExeption == false) {
                businessException = new AdapterException(ErrorType.BUSSINESS_ERROR, e, "接口服务端产生异常.");
            } else {
                businessException = (AdapterException) e;
            }
            
            out.setErrorType(businessException.getErrorType());
            out.setErrorMsg(businessException.getMessage());
            out.setBaseOutInfo(businessException.getInfoOutObj());
            StringBuilder paramSb = new StringBuilder();
            
            // 取得返回对象日志
            paramSb.append("errorType=").append(out.getErrorType())
                    .append(", errorMsg=").append(out.getErrorMsg());
            outParamStr = paramSb.toString();

            /*
             * 处理:异常错误记录处理.
             */
            logger.error(Messages.get(
                    MsgKey.ERROR_SYSTEM_STACK, new String[] { businessException.getExceptionStackTrace() }));
            paramRecord.doExceptionRecord(serialNum, businessException);
            
            /*
             * 处理:返回参数记录处理.
             */
            paramRecord.doOutParamRecord(
                    serialNum,
                    outParamStr);
            logger.info(Messages.get(MsgKey.GENERALSERVICE_OUTPARAM, new String[] { outParamStr }));
        }
        return outParamStr;
    }
    
    /**
     * 反射传入Bean对象.
     * @param className 传入Bean包名+类名
     * @return Bean对象
     */
    private Object doCreatInObject(String className) {
        Object obj = null;
        try {
            Class<?> c = Class.forName(className);
            obj = c.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    
    /**
     * 获取客户端IP地址.
     * 
     * @param serialNum 请求唯一序列号
     * @return IP地址
     */
    private String getClientIP(HttpServletRequest request, String serialNum) {
        try {
            if (request.getHeader("x-forwarded-for") == null) {
                return request.getRemoteAddr();
            }
            return request.getHeader("x-forwarded-for");
        } catch (Exception e) {
            // 异常错误记录
            AdapterException businessException = new AdapterException(e);
            paramRecord.doExceptionRecord(serialNum, businessException);
        }
        return "";
    }
    
    /**
     * 业务处理类实现.
     * 通过反射,实现业务对象
     * @param serviceName 业务实现类名
     * @return 业务实现类
     */
    private IBaseService initService(String serviceName) {
        
        String serviceId = "";
        boolean isBaffle = Boolean.parseBoolean(properties.getProperty(serviceName + BAFFLE_FIX));
        // true的场合, 运行挡板程序
        if (isBaffle) {
            // 测试挡板程序
            serviceId = serviceName + BAFFLE_FIX;
        } else {
            serviceId = serviceName;
        }
        IBaseService baseService = SpringContextHolder.getBean(serviceId);
        return baseService;
    }
}