实际使用记录,做个记录:
1、需要对方提供wsdl文件,该文件中有接口的Ip地址,方法名、参数等详细信息,
wsdl文档中targetNamespace为命名空间
<xsd:element name="searchBGDMIInfo">标签中name是方法名:文档以searchBGDMIInfo为例
详细可参考wsdl官方文档
wsdl文档例子:
2、测试与第三方平台网络是否可以正常连接,根据对方提供的wsdl文档,拼写curl命令,例:
-- 10.20.21.XXX第三方平台的IP地址,USERNAME标签账户,PASSWORD标签内容为密码,需要根据wsdl文档来,自定义命令
curl --request POST \--url 'http://10.20.21.XXX/newdiss/services/DMDataService?wsdl=' \--header 'Content-Type: text/xml' \--header 'content-type: application/xml' \--data '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><searchBGDMIInfo> <in0> <?xml version="1.0" encoding="utf-8"?><NACAOREQUEST><USERNAME>test</USERNAME><PASSWORD>123456</PASSWORD></NACAOREQUEST></in0> </searchBGDMIInfo></soap:Body>
</soap:Envelope>'
需要引入jar包
java"> <dependency><groupId>commons-discovery</groupId><artifactId>commons-discovery</artifactId><version>0.5</version></dependency><dependency><groupId>wsdl4j</groupId><artifactId>wsdl4j</artifactId><version>1.6.3</version></dependency><dependency><groupId>org.apache.axis</groupId><artifactId>axis</artifactId><version>1.4</version></dependency><dependency><groupId>javax.xml.rpc</groupId><artifactId>javax.xml.rpc-api</artifactId><version>1.1.1</version></dependency>
以下是java代码案例:
java">// searchBGDMIInfoUrl为平台Ip地址:http://10.20.xx.xxx:9000/newdiss/services/DMtestService
//zdMethod为方法名称:searchBGDMIInfo
// xmlStr_search为请求报文,xml格式,需要平台提供public String searchBGDMIInfo(){//获取文件列表String fileName="";String paramName="searchBGDMIInfo";SearchBGDMIInfoXmlRsp infoXmlRsp=new SearchBGDMIInfoXmlRsp();try {String xmlStr_search="<NACAOREQUEST>"+"<USERNAME>"+userName+"</USERNAME>"+"<PASSWORD>"+passWord+"</PASSWORD>"+"</NACAOREQUEST>";String wsdlurl = searchBGDMIInfoUrl;String method = zdMethod;String targetNamespace = zdTargetNamespace;Object[] keys = new Object[]{paramName};Object[] values = new Object[]{xmlStr_search};log.debug("searchBGDMIInfo 访问url => {} method => {} targetNamespace=> {} paramName参数名:{} ",wsdlurl,method,targetNamespace,paramName);String rspsXml = WebServiceUtils.sendSoap12(wsdlurl, method, targetNamespace,keys, values);log.debug("searchBGDMIInfo 返回响应rspsXml -=> {}", rspsXml);infoXmlRsp=xmlToBean(SearchBGDMIInfoXmlRsp.class,rspsXml);if(infoXmlRsp!=null&&infoXmlRsp.getEnvelope()!=null){fileName=infoXmlRsp.getEnvelope().getFileName();}log.debug("解析xml后 searchBGDMIInfo infoXmlRsp -=> {}", JSON.toJSONString(infoXmlRsp));} catch (Exception e) {log.error("searchBGDMIInfo exception", e);}return fileName;-------------------------------------/*** 调用WebService接口,AXIS方式直接引用远程的.wsdl文件* 调用接口时,传入方法名,字段与字段值顺序需要严格按照接口定义* 使用SOAP1.2发送消息** @param wsdlurl 接口地址* @param method 方法名* @param keys 字段(方法参数)* @param values 字段值(参数对应值)* @param targetNamespace 命名空间* @return 接口返回值*/public static String sendSoap12(String wsdlurl, String method, String targetNamespace, Object[] keys, Object[] values) throws Exception {Service service = new Service();Call call = (Call) service.createCall();call.setEncodingStyle("UTF-8");call.setTargetEndpointAddress(wsdlurl);call.setOperationName(new QName(targetNamespace, method)); // 方法名称call.setReturnType(XMLType.XSD_STRING);// 设置返回类型// call.setReturnClass(String[].class);params(targetNamespace, call, keys, values);call.setUseSOAPAction(true);// 设置超时时间call.setTimeout(SOCKET_TIMEOUT); // 设置超时时间为5//call.setSOAPActionURI(targetNamespace + "/" + method);call.setSOAPActionURI("");return (String) call.invoke(values);}/*** 调用WebService接口之前,方法参数及参数赋值* 当前接口中参数类型仅限于字符串、整数、布尔,参数为空的情况为字符串类型** @param targetNamespace 命名空间* @param call 接口访问请求* @param keys 字段(方法参数)* @param values 字段值(参数对应值)*/private static void params(String targetNamespace, Call call, Object[] keys, Object[] values) {if (keys == null || values == null) {return;}if (keys.length == 0 || values.length == 0) {return;}if (keys.length != values.length) {throw new IllegalArgumentException("接口方法参数与参数值不匹配!");}for (int i = 0; i < values.length; i++) {String key = (String) keys[i]; //方法参数Object value = values[i]; //参数值if (value == null) {call.addParameter(new QName(targetNamespace, key), XMLType.XSD_STRING, ParameterMode.IN);} else if (value instanceof String) {call.addParameter(new QName(targetNamespace, key), XMLType.XSD_STRING, ParameterMode.IN);} else if (value instanceof Integer) {call.addParameter(new QName(targetNamespace, key), XMLType.XSD_INTEGER, ParameterMode.IN);} else if (value instanceof Boolean) {call.addParameter(new QName(targetNamespace, key), XMLType.XSD_BOOLEAN, ParameterMode.IN);}}}
这样就可以测试了,也可以使用main方法来进行测试。