双向对账中涉及到调用外部的webservice接口
对端提供了的webservice接口,需要我们拼接xml数据,简直不要太坑了,不能发送一次请求,返回多个虚拟子账户的信息。每个子账户都要拼接一个xml,循环调用对端的接口。返回的xml数据经过解析,入库到我们的表中
1.循环账户信息,拼接请求参数
for(ElePayCheckParamsDto dto:gasList){
dto.setStartBankCountTime(elePayCheckParamsDto.getStartBankCountTime());
dto.setEndBankCountTime(elePayCheckParamsDto.getEndBankCountTime());
dto.setStartCountTime(elePayCheckParamsDto.getStartCountTime());
dto.setEndCountTime(elePayCheckParamsDto.getEndCountTime());
dto.setSettleAccountsDate(elePayCheckParamsDto.getSettleAccountsDate());
String xml = makeXml(dto);
try{
// http://10.238.2.179:8082/webservice/outService.ws?wsdl
Object[] objects = multiBankWSClient.invokeMethod(manyBanksInfoUrl,"outSystemWS",xml);
dto.setObjects(objects[0]);
}catch (Exception e){
logger.error("获取加油站id:"+dto.getGasId()+"对应的虚拟子账户:"+dto.getVirtualAccount()+"的账单异常报错信息:"+e);
}
}
private static String makeXml(ElePayCheckParamsDto elePayCheckParamsDto) {
String xml = "<MBS>\n" +
" <pub>\n" +
" <TENANTID>10001</TENANTID>\n" +
" <SRCOUTSYSTEMCODE>etc</SRCOUTSYSTEMCODE>\n" +
" <SRCBATCHNO>sandy20190314</SRCBATCHNO>\n" +
" <TRANSCODE>PBQB01</TRANSCODE>\n" +
" <TRANSDATETIME>"+DateUtils.getTimeStampStr(new Date())+"</TRANSDATETIME>\n" +
" <MD5>158cfd893b0598f078f7efa4e6b11696</MD5>\n" +
" </pub>\n" +
" <req>\n" +
" <head>\n" +
" <COMMANDCODE>GETBANKACCOUNTSTRANS</COMMANDCODE>\n" +
" <PAGEINDEX>1</PAGEINDEX>\n" +
" <PAGESIZE>100</PAGESIZE>\n" +
" </head>\n" +
" <searchconditions>\n" +
" <TRADEDATESTART>"+DateUtils.getReqDate(elePayCheckParamsDto.getStartBankCountTime())+"</TRADEDATESTART>\n" +
" <TRADEDATEEND>"+DateUtils.getReqDate(elePayCheckParamsDto.getEndBankCountTime())+"</TRADEDATEEND>\n" +
" <BANKACCOUNTNUMBER>"+elePayCheckParamsDto.getVirtualAccount()+"</BANKACCOUNTNUMBER>\n" +
" </searchconditions>\n" +
" </req>\n" +
" </MBS>";
}
2.最开始我是使用的Client通道进行请求接口(也是对端文档提供的方法),磊哥看了一下我的代码,觉得不够优化,然后给我整了一个公共类
我最开始使用的方法:
Client client=null;
for(ElePayCheckParamsDto dto:gasList){
dto.setStartBankCountTime(elePayCheckParamsDto.getStartBankCountTime());
dto.setEndBankCountTime(elePayCheckParamsDto.getEndBankCountTime());
dto.setStartCountTime(elePayCheckParamsDto.getStartCountTime());
dto.setEndCountTime(elePayCheckParamsDto.getEndCountTime());
dto.setSettleAccountsDate(elePayCheckParamsDto.getSettleAccountsDate());
String xml = makeXml(dto);
try{ // http://10.238.2.179:8082/webservice/outService.ws?wsdl
client=new Client(new URL(manyBanksInfoUrl)); //manyBanksInfoUrl
logger.info("请求保融入参:{}" + xml);
Object[] objects=client.invoke("outSystemWS",new String[]{xml});
logger.info("请求保融返回:{}" + objects[0]);
dto.setObjects(objects[0]);
}catch (Exception e){
logger.error("获取加油站id:"+dto.getGasId()+"对应的虚拟子账户:"+dto.getVirtualAccount()+"的账单异常报错信息:"+e);
}
}
if(null != client){
client.close();
}
磊哥帮忙优化之后的
引入的相关jar包有
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
双向对账的时候,一定要注意先和对端调好接口。。接口调通又数据十分重要的