数据传输格式报文格式: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;
}
}




征信接口调用,解析(xml)的更多相关文章

  1. java短信接口调用

    java短信接口调用 之前一直在一个传统的单位上班好多听容易的技术都没接触过,即使有时候想搞一搞类似于支付宝支付,短信接口调用,微信公众号,小程序之类等功能,一直有心无力终于跳槽了,估计是氛围的原因吧 ...

  2. PJzhang:exiftool图片信息提取工具和短信接口调用工具TBomb

    猫宁!!! 作者:Phil Harvey 这是图片信息提取工具的地址: https://sno.phy.queensu.ca/~phil/exiftool/ 网站隶属于Sudbury 中微子天文台,从 ...

  3. asp.net mvc短信接口调用——阿里大于API开发心得

    互联网上有许多公司提供短信接口服务,诸如网易云信.阿里大于等等.我在自己项目里需要使用到短信服务起到通知作用,实际开发周期三天,完成配置.开发和使用,总的说,阿里大于提供的接口易于开发,非常的方便,短 ...

  4. 短信接口调用以及ajax发送短信接口实现以及前端样式

    我们短信api用的是云信使平台提供的非免费短信服务:官网提供的demo有两种,分别是function加其调用.class文件加其调用. 在这里我们用class文件加调用: 首先,ThinkPHP里面自 ...

  5. zabbix短信接口调用

    #!/bin/bash TIME=`date +%Y-%m-%d` KEY="UJK9rk50HD8du8JE8h87RUor0KERo5jk" username="za ...

  6. 模板短信接口调用java,pythoy版(一) 网易云信

    说明 短信服务平台有很多,我只是个人需求,首次使用,算是测试用的,故选个网易(大公司). 稳定性:我只测试了15条短信... 不过前3条短信5分钟左右的延时,后面就比较快.... 我只是需要发短信,等 ...

  7. 模板短信接口调用java,pythoy版(二) 阿里大于

    说明 功能:短信通知发送 + 短信发送记录查询,所有参数我没有改动,实测有效! 请自行参考 + 官方API! 短信模板示例:尊敬的${name},您的快递已在飞奔的路上,将在今天${time}送达您的 ...

  8. 中国网建SMS短信接口调用(java发送和接收手机短信)

    1.先注册账号,一定要填写好签名格式.不填会返回-51错误.   代码信息接口详细==>http://sms.webchinese.cn/api.shtml   . 2.测试代码 package ...

  9. .net短信接口调用示例(106短信通道)

    1. [代码]调用代理示例 using System;using System.Data;using System.Configuration;using System.Collections;usi ...

随机推荐

  1. ArcEngine几何变换中的策略模式

    使用策略模式可以减少分支语句,switch...Case,同时便于策略的扩展. 1. ITransform2D接口的Transform方法: [C#]public void Transform ( e ...

  2. 【转】Deep Learning(深度学习)学习笔记整理系列之(七)

    9.5.Convolutional Neural Networks卷积神经网络 卷积神经网络是人工神经网络的一种,已成为当前语音分析和图像识别领域的研究热点.它的权值共享网络结构使之更类似于生物神经网 ...

  3. 基于struts2--实现文件上传下载

    1. 文件的上传: 1). 表单需要注意的 3 点 ①. method="post"     ②. enctype="mulitipart/form-data" ...

  4. redis客户端hiredis

    Hiredis 在官网 http://redis.io/clients 中有说明This is the official C client. Support for the whole command ...

  5. CentOS7 忘了root密码怎么办

    今天打开很久没开机的另2台虚拟机,发现不记得root密码了. 于是参考了google搜索到的第一个答案,https://www.unixmen.com/reset-root-password-cent ...

  6. centos配置用户级别的jdk的环境变量

    前面讲解了centos配置jdk的环境变量 的root级别的jdk配置 ,这里讲解用户级别的jdk配置. 在用户的当前目录下,如下,有四个隐藏的文件,文件打头是.bash******: 1.编辑.ba ...

  7. PHP保存Base64图片base64_decode的问题 文件打不开的问题

      PHP对Base64的支持非常好,有内置的base64_encode与base64_decode负责图片的Base64编码与解码. 编码上,只要将图片流读取到,而后使用base64_encode进 ...

  8. android系统提供的几种颜色Color

    http://blog.csdn.net/feiyangxiaomi/article/details/38338305 记录一下android自带颜色. Constants public static ...

  9. Vue学习笔记之Vue组件

    0x00 前言 vue的核心基础就是组件的使用,玩好了组件才能将前面学的基础更好的运用起来.组件的使用更使我们的项目解耦合.更加符合vue的设计思想MVVM. 那接下来就跟我看一下如何在一个Vue实例 ...

  10. SQL学习笔记五之MySQL索引原理与慢查询优化

    阅读目录 一 介绍 二 索引的原理 三 索引的数据结构 四 聚集索引与辅助索引 五 MySQL索引管理 六 测试索引 七 正确使用索引 八 联合索引与覆盖索引 九 查询优化神器-explain 十 慢 ...