zoukankan      html  css  js  c++  java
  • 征信接口调用,解析(xml)

    数据传输格式报文格式:xml
    public CisReportRoot queryCisReport(PyQueryBean pyQueryBean) throws Exception {
    CisReportRoot cisReportRoot = invokePy(pyQueryBean){
    CisReportRoot cisReportRoot = queryCisReportFromPyServer(pyQueryBean);   } } 
    
    pyQueryBean--CisReportRoot 
    注意:接口数据传输通过xml,无论发送请求,还是获取响应都需要经过经过xml格式转化。 
    1,CisReportRoot cisReportRoot = getCisReportRoot(doc); pyQueryBean--Map--doc--xmL
    //实体转化为Map 2,Map<String, String> map = CommonUtils.beanToMap(pyQueryBean);
    //Map转化为指定标签的xml字符串 Map-doc-xml 3,String queryInfo = XmlUtil.createQueryCondition(map);
    //从鹏元获取Xml,转化为doc xml-doc- 4,Document doc = pyClient.connectToPyClient(queryInfo);
    //解析doc中数据,赋值到实体
    5,CisReportRoot cisReportRoot = getCisReportRoot(doc); 具体实现如下:
    2, public static Map<String, String> beanToMap(Object obj) { if (obj == null) { return null; } Map<String, String> map = new HashMap<>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); // 过滤class属性 if (!"class".equals(key)) { // 得到property对应的getter方法 Method getter = property.getReadMethod(); Object value = getter.invoke(obj); map.put(key, String.valueOf(value)); } } } catch (Exception e) { System.out.println("transBean2Map@CommonUtils_Exception" + e); } return map; } 3, 构建请求xml public static String createQueryCondition(Map<String, String> map) throws Exception { Document document = DocumentHelper.createDocument(); document.setXMLEncoding(ENCODING); Element root = document.addElement("conditions"); Element conditionElement = root.addElement("condition"); conditionElement.addAttribute("queryType", map.get("queryType")); Element itemName = conditionElement.addElement("item"); itemName.addElement("name").addText("name"); itemName.addElement("value").addText(map.get("name") == null ? "" : map.get("name")); Element itemDocumentNo = conditionElement.addElement("item"); itemDocumentNo.addElement("name").addText("documentNo"); itemDocumentNo.addElement("value").addText(map.get("documentNo") == null ? "" : map.get("documentNo")); Element itemPhone = conditionElement.addElement("item"); itemPhone.addElement("name").addText("phone"); itemPhone.addElement("value").addText(map.get("phone") == null ? "" : map.get("phone")); Element itemQueryReasonID = conditionElement.addElement("item"); itemQueryReasonID.addElement("name").addText("queryReasonID"); itemQueryReasonID.addElement("value").addText(map.get("queryReasonID") == null ? "" : map.get("queryReasonID")); Element itemSubreportIDs = conditionElement.addElement("item"); itemSubreportIDs.addElement("name").addText("subreportIDs"); itemSubreportIDs.addElement("value").addText(map.get("subreportIDs") == null ? "" : map.get("subreportIDs")); Element itemRefID = conditionElement.addElement("item"); itemRefID.addElement("name").addText("refID"); itemRefID.addElement("value").addText(map.get("refID") == null ? "" : map.get("refID")); return document.asXML(); } 4,获取鹏飞响应Xml /** * 连接到鹏飞服务端 * * @param queryInfo 查询信息--Xml格式 * @return 返回查询结果 */ public Document connectToPyClient(String queryInfo) throws Exception { try { Client client = new Client(new URL(configInfo.getPy_ssl_ip())); Object[] results = client.invoke(QUERY_REPORT, new Object[]{configInfo.getPy_username(), configInfo.getPy_password(), queryInfo, "xml"}); Document dom4j; if (results[FIRST_NODE] instanceof org.w3c.dom.Document) { //FIRST_NODE = 0 org.w3c.dom.Document doc = (org.w3c.dom.Document) results[FIRST_NODE]; org.w3c.dom.Element element = doc.getDocumentElement(); //获取所有子节点 NodeList children = element.getChildNodes(); //获取第一个子节点 //xml to dom dom4j = XmlUtil.getDoc(children.item(FIRST_NODE).getNodeValue()); //获取根节点 Element root = dom4j.getRootElement(); Element statusElement = root.element(STATUS); String ba64; if (statusElement.getData().equals(SUCCESS)) { /*客户端获取该returnValue值后需要依次做以下工作: 1、 把returnValue值用Base64解码,把字符串(string)转成字节流(byte[]); 2、 把字节流解压缩后,获取到相应的多个输出文件,其中html和pdf是压缩文件; 3、 解析reports.xml获取xml报告的详细内容;*/ //1、 把returnValue值用Base64解码,把字符串(string)转成字节流(byte[]); ba64 = root.element(RETURN_VALUE).getData().toString(); byte[] re = Base64Utils.decode(ba64); //2、 把字节流解压缩后,获取到相应的多个输出文件,其中html和pdf是压缩文件; String xml = CompressStringUtil.decompress(re); logger.info(xml); //3、 解析reports.xml获取xml报告的详细内容; return XmlUtil.getDoc(xml); } else { StringBuilder sb = new StringBuilder(PyCreditServiceErrorEnum.PY_SYS_ERROR.getMsg()); String xml = XmlUtil.getString(dom4j); logger.error(xml); if (null != root.element("errorMessage")) { sb.append(", ").append(root.element("errorMessage").getData().toString()); logger.error("py server has problem, errorMessage: " + sb.toString()); } throw new CreditException(PyCreditServiceErrorEnum.PY_SYS_ERROR.getCode(), sb.toString()); } } return null; } catch (SocketTimeoutException e) { logger.error("level0_loadClient@PyClientService_Exception", e); throw new CreditException(PyCreditServiceErrorEnum.PY_SYS_TIMEOUT.getCode(), PyCreditServiceErrorEnum.PY_SYS_TIMEOUT.getMsg()); } catch (Exception e) { logger.error("level0_connectToPyClient@ConnectToPyClient_Exception", e); throw e; } } 5,解析xml报告中数据
    CisReportRoot cisReportRoot = getCisReportRoot(doc);
    总括:解析doc每一层节点,获取属性,当前子节点数据,以此类推分层解析。
    思路:属性,子节点List-map-通过反射给实体赋值
    /**
         * @return com.pingan.credit.model.py.CisReportRoot
         * @Description: 解析报告里的数据
         * @author chiyuanzhen743
         * @date 2017/8/24 14:53
         */
        private CisReportRoot getCisReportRoot(Document doc) throws Exception {
            CisReportRoot cisReportRoot;
            Element root = doc.getRootElement();
         //(1)获取根节点属性 cisReportRoot
    = getRootAttribute(root); Element cisReport = root.element("cisReport"); CisReportChild crc = new CisReportChild(); // 获取报告节点的所有属性 if (!ListUtil.isEmpty(cisReport.attributes())) { List<Attribute> eRootAttributeList = (List<Attribute>) cisReport.attributes(); crc = getChildReportAttribute(eRootAttributeList); } ReportElement re = new ReportElement(); //(2)获取报告所有子节点数据 re = getCisReportChild(cisReport, re); crc.setReportElement(re); cisReportRoot.setCisReportChild(crc); return cisReportRoot; }
    //(1)获取根节点属性
    /**
         * 获取根节点所有属性
         *
         * @param root 根节点
         * @return 报告根节点对象
         */
        private CisReportRoot getRootAttribute(Element root) throws Exception {
            List<Attribute> attributeList = root.attributes();
            Map<String, String> result = attributeList.stream()
                    .collect(Collectors.toMap(Attribute::getName, Attribute::getValue));
            CisReportRoot cisreportRoot = new CisReportRoot();
            CommonUtils.setValue(cisreportRoot, result);
            return cisreportRoot;
        }
    //(2)获取报告所有子节点数据
        /**
         * @param cisReport, re
         * @return com.pingan.credit.model.py.ReportElement
         * @Description: 获取报告中 每个节点的数据
         * @date 2017/10/23 20:25
         */
        private ReportElement getCisReportChild(Element cisReport, ReportElement re)
                throws Exception {
    
            // 2.获取身份认证 policeCheckInfo
            Element policeCheckInfoElement = cisReport.element("policeCheckInfo");
         //(3)获取节点属性 List
    <Attribute> policeCheckInfoAttributeList = policeCheckInfoElement.attributes();
         //通过反射给实体赋值 PoliceCheckInfo pci
    = getPoliceCheckInfoNode(policeCheckInfoElement, policeCheckInfoAttributeList); re.setPoliceCheckInfo(pci);
    //(3)获取节点属性
    xml样例
    <policeCheckInfo subReportType="10602" subReportTypeCost="96040" treatResult="1" errorMessage="">
    <item>
        <name>测试一</name>
        <documentNo>14043019900217914X</documentNo>
        <result>1</result>
    </item>
    </policeCheckInfo>
     
     /**
         * @param policeCheckInfoElement, policeCheckInfoAttributeList
         * @return com.pingan.credit.model.py.PoliceCheckInfo
         * @Description: 身份认证节点
         * @author chiyuanzhen743
         * @date 2017/10/20 15:13
         */
        private PoliceCheckInfo getPoliceCheckInfoNode(Element policeCheckInfoElement,
                                                       List<Attribute> policeCheckInfoAttributeList) throws Exception {
            try {
                // 获取子节点的全部属性
                Map<String, String> attributeMap = policeCheckInfoAttributeList.stream()
                        .collect(Collectors.toMap(Attribute::getName, Attribute::getValue));
           //(4)获取属性给父类赋值,因为节点属性是相同的,抽象为父类
                PoliceCheckInfo pci = (PoliceCheckInfo) CommonUtils.setValueOfSuperClass(PoliceCheckInfo.class, attributeMap);
                if (SUCCESS.equals(attributeMap.get("treatResult"))) {
                    // 获取身份认证内容节点
                    List<Element> policeCheckInfoList = policeCheckInfoElement.element("item").elements();
              //(5)节点内容转化为map Map
    <String, String> resultMap = XmlUtil.getResultMap(policeCheckInfoList);
              //(6)反射赋值 pci
    = (PoliceCheckInfo) CommonUtils.setValue(pci, resultMap); } else { throw new CreditException(PyCreditServiceErrorEnum.IDENTITY_NOT_MATCH.getCode(), PyCreditServiceErrorEnum.IDENTITY_NOT_MATCH.getMsg()); } return pci; } catch (Exception e) { logger.error("getPoliceCheckInfoNode@PyServiceImpl_Expection", e); throw e; } }
    //(4)获取属性给父类赋值,因为节点属性是相同的,抽象为父类。
    /**
         * @param clazz, attributeMap
         * @return java.lang.Object
         * @Description: 通过反射给目标对象的父类设置属性
         * @date 2017/8/30 9:42
         */
        public static Object setValueOfSuperClass(Class<?> clazz, Map<String, String> attributeMap) throws Exception {
            try {
                Object object = Class.forName(clazz.getName()).newInstance();
                Class<?> obj = object.getClass().getSuperclass();
                Field[] fields = obj.getDeclaredFields();
                setValue(object, attributeMap, fields);
                return object;
            } catch (Exception e) {
                logger.info(e.getMessage());
                throw e;
            }
        }

    //(5)节点内容转化为map

     /**
         * 将元素节点转换成map
         */
        public static Map<String, String> getResultMap(List<Element> items) {
            Map<String, String> map = new HashMap<>(32);
            for (Element e : items) {
                if (StringUtils.isNotEmpty(e.getData().toString())) {
                    map.put(e.getName(), e.getData().toString());
                }
            }
            return map;
        }

    //(6)反射赋值

        /**
         * @param object, resultMap
         * @return java.lang.Object
         * @Description: 通过反射为实体类赋值
         * @date 2017/8/30 9:43
         */
        public static Object setValue(Object object, Map<String, String> resultMap) throws Exception {
            try {
                Class<?> obj = object.getClass();
                Field[] fields = obj.getDeclaredFields();
                setValue(object, resultMap, fields);
    
                return object;
            } catch (IllegalAccessException e) {
                logger.info("IllegalAccessException@CommonUtils:" + e.getMessage());
                throw e;
            } catch (Exception e) {
                logger.info("Exception@CommonUtils:" + e.getMessage());
                throw e;
            }
        }
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    20180925-7 规格说明书-吉林市2日游
    第二周例行报告psp
    20180918-1 词频统计
    第一周例行报告psp
    第一周博客作业2018091-2
    20181009-9 每周例行报告
    20180925-1 每周例行报告
    20180925-7 规格说明书-吉林市2日游
    20180925-5 代码规范,结对要求
    20180925-4 单元测试,结对
  • 原文地址:https://www.cnblogs.com/shenkebky/p/8302416.html
Copyright © 2011-2022 走看看