Java接口使用xml配置进行数据校验
包含内容: 源码,全套工具
作者QQ549710689
我们在开发业务系统时,少不的就是系统与系统间的交互, 一般是一个系统提供接口供另一个系统进行调用, 如A系统调用B系统的接口, A系统将接口需要的数据传入B, B根据相应的数据进行对应业务的处理, 但为了业务能够正常的运行, B系统会对A传入的数据进行校验, 为了使校验能够模块和标准化, B系统可以提供统一的配置和标准进行校验, 今天我们就实现一种方法, A传入数据后, B获取数据, 然后根据数据中的码值来调用xml配置进行标准化校验,校验成功后再执行相应的业务, 如果不成功,则回传错误信息
项目对应的实例代码可以通过右侧【下载实例】按钮获取
开发工具: Eclipse Mars, JDK1.7, Tomcat7
【项目包含内容】(见下图):
【DataValid】 源码
【接口文档.xlsx】 接口文档
【项目配置】
1. 将DataValid导入到Eclipse Mars中,JDK配置为JDK1.7, tomcat选择tomcat7
2. 接口文档.xlsx 中有相应数据标准格式, 代码是对数据格式的实现
【代码流程讲解】
1. A系统将数据按照上图的标准组装成json格式传给B系统
2. B系统获取json数据, 提取数据中的 transCode
3. B系统根据transCode组装校验数据的xml名称, transCode与xml校验文件是一一对应的
校验的xml内容如下: 判断内容是否为空; 校验长度; 校验时间格式等等
<tran name="WL01" type="LoanDTO" remark="还款申请"> <params> <param name="extLoanNo" required="true" type="String" maxLength="100" remark="五星申请贷款唯一标识号"></param> <param name="transArea" required="true" type="String" maxLength="6" remark="本笔贷款业务发生的地区"></param> <param name="applAmt" required="true" type="Number" maxLength="16" decimal="2" remark="申请金额"></param> <param name="applDt" required="true" type="String" maxLength="8" format="YYYYMMDD" remark="申请日期"></param> <param name="applPerd" required="true" type="Number" maxLength="3" remark="申请期限(月)"></param> <param name="banPerd" required="false" type="String" maxLength="3" remark="贷款禁还期(天)"></param> <param name="custName" required="true" type="String" maxLength="120" remark="客户名称"></param> <param name="custType" required="true" type="String" maxLength="1" contentType="0" remark="客户类型"></param> <param name="paperType" required="true" type="String" maxLength="2" remark="证件类型"></param> <param name="paperNo" required="true" type="String" maxLength="30" remark="身份证号"></param> <param name="paperVaildDate" required="true" type="String" maxLength="8" format="YYYYMMDD" remark="证件有效期"></param> <param name="phoneNo" required="true" type="String" maxLength="20" format="mobilePhone" remark="手机号"></param> <param name="address" required="true" type="String" maxLength="200" remark="住址"></param> <param name="recvAccBnk" required="true" type="String" maxLength="12" remark="客户收款开户行"></param> <param name="recvAccName" required="true" type="String" maxLength="120" remark="客户收款户名"></param> <param name="recvAccNo" required="true" type="String" maxLength="40" remark="客户收款银行账号"></param> <param name="repyAccBnk" required="false" type="String" maxLength="12" remark="客户还款开户行"></param> <param name="repyAccName" required="false" type="String" maxLength="120" remark="客户还款户名"></param> <param name="repyAccNo" required="false" type="String" maxLength="40" remark="客户还款银行账号"></param> <param name="extLoanUse" required="true" type="String" maxLength="50" remark="贷款用途"></param> <param name="guarList" required="false" type="List" minSize="0" maxSize="360" remark="担保人列表"> <params> <param name="guatCustName" required="true" type="String" maxLength="120" remark="担保人名称"></param> <param name="guatCustType" required="true" type="String" maxLength="1" remark="担保人类型"></param> <param name="guatPaperType" required="true" type="String" maxLength="2" remark="担保人证件类型"></param> <param name="guatPaperNo" required="true" type="String" maxLength="30" decimal="6" remark="担保人证件号码"></param> <param name="guatPhoneNo" required="true" type="String" maxLength="16" decimal="6" remark="担保人联系电话"></param> <param name="guatContactName" required="false" type="String" maxLength="120" remark="担保人联系人姓名"></param> <param name="guatContactPhoneNo" required="false" type="String" maxLength="20" remark="担保人联系人手机"></param> </params> </param> <param name="intStartDt" required="true" type="String" maxLength="8" format="YYYYMMDD" remark="起息日"></param> <param name="lastDueDt" required="true" type="String" maxLength="8" format="YYYYMMDD" remark="止息日"></param> <param name="gracePerd" required="true" type="Integer" maxLength="3" remark="本金利息宽限期"></param> <param name="intRate" required="true" type="Number" maxLength="10" decimal="6" remark="年利率%"></param> <param name="odIntRate" required="true" type="Number" maxLength="10" decimal="6" remark="罚息年利率%"></param> <param name="mainGurTyp" required="true" type="String" maxLength="1" remark="主担保方式"></param> <param name="repayTyp" required="true" type="String" maxLength="2" remark="还款方式"></param> <param name="capitalDirection" required="true" type="String" maxLength="5" remark="资金投向"></param> <param name="fileList" required="false" type="List" minSize="0" maxSize="360" remark="附件列表"> <params> <param name="fileMD5" required="true" type="String" maxLength="32" remark="文件MD5"></param> <param name="fileRName" required="true" type="String" maxLength="50" remark="文件真实名称"></param> <param name="fileVName" required="true" type="String" maxLength="100" remark="weedfs文件名"></param> <param name="filePath" required="true" type="String" maxLength="100" remark="weedfs文件路径"></param> <param name="fileSize" required="true" type="String" maxLength="100" remark="文件大小"></param> </params> </param> </params> </tran>