关于axis怎样加入Header信息
这几天遇到一个需求,要调用cxf发布的一个webservice,但客户端调用时需要传入soapenv:Header参数,xml格式如下:
<soapenv:Envelope >
<soapenv:Header>
<tns:RequestSOAPHeader xmlns:tns="http://webservice.xxx.com/" >
<tns:user>abc</tns:user>
<tns:password>c7bb9ff815174523e3d9234</tns:password>
</tns:RequestSOAPHeader>
</soapenv:Header>
<soapenv:Body>
消息体...
</soapenv:Body>
</soapenv:Envelope>
其中的难点是怎么传入soapenv:Header信息。因为客户端系统时间比较久,发现用cxf的方式调用可以成功,但要增加jar包,故放弃,继后考虑到原系统用的axis故在网上查了下实现方式。
1,wsdl2java生成客户端代码。其axis的写法是:
java -classpath %AXIS_CLASSPATH% org.apache.axis.wsdl.WSDL2Java -o D:\backup -p com.cn.sany.bmp http://bpm.abc.com/45rtyFGHqw/sys/webservice/loginqwWebserviceService?wsdl
其中,AXIS_CLASSPATH需要的包有:
%AXIS_LIB%\axis.jar;%AXIS_LIB%\axis-ant.jar;%AXIS_LIB%\axis-schema.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.1.1.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\log4j-1.2.13.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\wsdl4j-1.6.2.jar;%AXIS_LIB%\activation.jar;%AXIS_LIB%\mail.jar;%AXIS_LIB%\xmlsec-1.4.7.jar;
特别的是,在axis-bin-1_3.zip中不包括activation.jar、mail.jar和xmlsec-1.3.0.jar这3个文件,需要去SUN和Apache主页单独下载,
分别是jaf-1_0_2-upd2.zip、javamail-1_4.zip和xml-security-bin-1_3_0.zip。
注:如果是cfx方式,更简单:
"D:\apache-cxf-2.5.2\bin\wsdl2java" -fe jaxws21
-p com.sany.account.webservice -d client
-client http://10.0.15.112:18080/abcInterface/service/AccountantVoucherSynServer?wsdl
2,写单元测试类,方法如下:
ILoginWebserviceServiceServiceLocator locator = new ILoginWebserviceServiceServiceLocator();
ILoginWebserviceServiceServiceSoapBindingStub service = null;
service = (ILoginWebserviceServiceServiceSoapBindingStub) locator.getILoginWebserviceServicePort(
new java.net.URL("http://bpm.abc.com/45rtyFGHqw/sys/webservice/loginqwWebserviceService?wsdl"));
SOAPHeaderElement header = new SOAPHeaderElement("http://webservice.xxx.com/","RequestSOAPHeader");
SOAPElement soap = null;
soap = header.addChildElement("tns:user");
soap.addTextNode("abc");
soap = header.addChildElement("tns:password");
soap.addTextNode("c7bb9ff815174523e3d980faa2");
service.setHeader(header);
LoginParameterForm form = new LoginParameterForm();
form.setLoginName("wangdw10");
LoginWebserviceResult result = null;
result = service.getLoginSessionId(form);
这样就能达到目的了!