场景:合作方服务器接口为webservice 方式,需使用soap方式调用
SoapUI调用测试步骤:
1.创建工程
2.请求接口,可以看到调用接口需要传的参数结构,以及接口返回的具体参数结构
后台使用axis2封装xml信息->请求合作方->解析返回参数:
ServiceClient sender = new ServiceClient();
String serviceUrl = "wsdl地址";
String targetNameSpace = "浏览器访问wsdl地址拿到targetNamespace";
String policyInfoXml = getPolicyInfoXml(orderInfo);// StringBuffer封装xml信息
String opName = "approval";// 要调用的方法
Options options = new Options();
EndpointReference targetEPR = new EndpointReference(serviceUrl);
options.setTo(targetEPR);
options.setAction(targetNameSpace + "/" + opName);
sender.setOptions(options);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(targetNameSpace, "");
OMElement method = fac.createOMElement(opName, omNs);
OMElement request = fac.createOMElement("request", omNs);
OMElement policyInfo = fac.createOMElement("policyInfo", omNs);
policyInfo.setText(getPolicyInfoXml(orderInfo));
OMElement checkCode = fac.createOMElement("checkCode", omNs);
checkCode.setText(getCheckCode(policyInfoXml));
OMElement formCommit = fac.createOMElement("formCommit", omNs);
formCommit.setText("true");
OMElement productInfo = fac.createOMElement("productInfo", omNs);
OMElement classesCode = fac.createOMElement("classesCode", omNs);
classesCode.setText("12040100");
productInfo.addChild(classesCode);
OMElement userInfo = fac.createOMElement("userInfo", omNs);
OMElement password = fac.createOMElement("password", omNs);
password.setText(getPassWord());
OMElement userName = fac.createOMElement("userName", omNs);
userName.setText(getUserName());
userInfo.addChild(userName);
userInfo.addChild(password);
request.addChild(checkCode);
request.addChild(policyInfo);
request.addChild(formCommit);
request.addChild(productInfo);
request.addChild(userInfo);
method.addChild(request);
method.build();
// 发送参数至合作商
OMElement resp = sender.sendReceive(method);
logger.info("返回响应结果:" + resp);
String resultXml = StringEscapeUtils.unescapeXml(resp.toString());
// 解析<ns1:sysMessage>
int sysStart = resultXml.indexOf("<ns1:sysMessage>");
int sysEnd = resultXml.indexOf("</ns1:sysMessage>");
if (sysStart == -1 || sysEnd == -1) {
logger.error("返回参数错误,<ns1:sysMessage>不存在");
return result;
}
String sysMessageStr = resultXml.substring(sysStart,
sysEnd + "</ns1:sysMessage>".length()).replaceAll("ns1:", "");
// xml->map
Map<String, Object> sysMap = XmlUtil.xml2Map(sysMessageStr); //xml2Map转换方法见https://www.cnblogs.com/yfzhou528/p/12147357.html
if (sysMap == null || sysMap.size() == 0) {
logger.error("返回响应结果-<ns1:sysMessage>解析失败");
return result;
}
String resultStatus = (String) sysMap.get("resultStatus");
String errorCode = (String) sysMap.get("errorCode");
String errorMsg = (String) sysMap.get("errorMsg");
if (StringUtil.isBlank(resultStatus) || !"S|F".contains(resultStatus)) {
logger.error("返回响应结果-resultStatus参数错误");
return result;
}
// 解析policyInfo携带的xml
int rootStart = resultXml.indexOf("<ROOT>");
int rootEnd = resultXml.indexOf("</ROOT>");
if (rootStart == -1 || rootEnd == -1) {
logger.error("返回响应结果参数错误,<ROOT>不存在");
return result;
}
String rootStr = resultXml.substring(rootStart,
rootEnd + "</ROOT>".length());
// xml->map
Map<String, Object> rootMap = XmlUtil.xml2Map(rootStr);
String ApplyId = (String) rootMap.get("ApplyId");
String STATUS = (String) rootMap.get("STATUS");
String APPLYNO = (String) rootMap.get("APPLYNO");
String POLICYNO = (String) rootMap.get("POLICYNO");
String FILE_URL = (String) rootMap.get("FILE_URL");