Axi2学习总结及如何在client端加入header头
最近项目用到WebService 要求用Axis2实现,以前没用过Axis2,最近把网上的资料加上自己的琢磨写在这里,网上大多介绍了服务端Service简单的传递字符串 和数组对象,没具体介绍传递对象里封装嵌套简单对象和数组对象,类似如下对象:
?好了服务端生成好了 直接访问:http://127.0.0.1:8090/HelloWorld/services/Hello?wsdl?看是否发布成功;
客户端代码实现如下:客户端也是用axis2实现的,axis2实现的客户端调用方式比较多,为了能满足对象里封装对象数组和简单对象 的需求,我采用了stub ,用wsdl2java方式生成,命令如下:
package client;import java.rmi.RemoteException;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMNamespace;import org.apache.axiom.soap.impl.dom.SOAPHeaderBlockImpl;import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;import org.apache.axiom.soap.impl.dom.soap11.SOAP11HeaderBlockImpl;public class AddHeaderClinet { public static void main(String args[]) throws RemoteException, AppAxis2Exception{ HelloStub stub = new HelloStub(); HelloStub.GetList gg = new HelloStub.GetList(); HelloStub.Prameters param = new HelloStub.Prameters(); param.setCode("test"); param.setCode2("hello "); gg.setP(param); SOAP11Factory factory = new SOAP11Factory(); OMNamespace SecurityElementNamespace = factory.createOMNamespace( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse"); OMElement usernameTokenEl = factory.createOMElement("UsernameToken", SecurityElementNamespace); OMElement usernameEl = factory.createOMElement("Username", SecurityElementNamespace); OMElement passwordEl = factory.createOMElement("Password", SecurityElementNamespace); OMElement actionEl = factory.createOMElement("Action", SecurityElementNamespace); passwordEl.addAttribute(factory.createOMAttribute("Type", null, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText")); usernameEl.setText(""); passwordEl.setText(""); usernameTokenEl.addChild(usernameEl); usernameTokenEl.addChild(passwordEl); usernameTokenEl.addChild(actionEl); SOAPHeaderBlockImpl block = new SOAP11HeaderBlockImpl("Security", SecurityElementNamespace, factory); block.addChild(usernameTokenEl); //加入head头 stub._getServiceClient().addHeader(block); //调用 HelloStub.BuissnessInfo info = stub.getList(gg).get_return(); //返回 String ss =info.getCode(); HelloStub.User[] strArray = info.getUser(); for (HelloStub.User s : strArray) System.out.println("id="+s.getId()+"name="+s.getName()); System.out.println(ss); }}?
以上都是我在网上查询的资料整合,希望对大家有需求的有帮助,谢谢,希望共同探讨,同时,,我正对AXIS2在服务端对Header头报文的验证 很郁闷!!!!,期望 有高手给指点下,谢谢