java2wsdl+Axis2+hibernate开发webservice学习(3) - 定制soap header

soap header一般用户做验证使用,定制soap header一般有3中方式:

 

第一种:add simple string

 

   --- on stub

 

        _stub = new OnepassServicesStub( "http://192.168.1.147:8080/mywebservice/services/OnepassServices?wsdl"); OMFactory omFactory =OMAbstractFactory.getOMFactory(); OMNamespace omNamespace = omFactory.createOMNamespace("http://mycompany.org", "myHeader"); OMElement header = omFactory.createOMElement("header", omNamespace); header.setText("This is a custom soap header"); _stub._getServiceClient().addHeader(header);

 

   --- AuditInHandler.java

        soap message:

 

        <?xml version='1.0' encoding='utf-8'?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Header> <myHeader:header xmlns:myHeader="http://mycompany.org"> This is a custom soap header </myHeader:header> </soapenv:Header> <soapenv:Body> <ns2:getUserProfile xmlns:ns2="http://axis.test.com/ws" /> </soapenv:Body> </soapenv:Envelope>

 

 

        <myHeader:header xmlns:myHeader="http://mycompany.org">
              This is a custom soap header

        </myHeader:header>-----------------------------------------------------------custom header

 

 

第二种:用OMElement object封装soap header

 

       --- on stub

 

       OMElement header1 = AXIOMUtil .stringToOM("<header1><systemId>system-011</systemId></header1>"); OMElement header2 = AXIOMUtil.stringToOM("<header2><requestTime>" + new java.util.Date() + "</requestTime></header2>"); _stub._getServiceClient().addHeader(header1); _stub._getServiceClient().addHeader(header2);

 

       --- soap message

 

       <?xml version='1.0' encoding='utf-8'?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Header> <header1> <systemId>system-011</systemId> </header1> <header2> <requestTime>Mon Jan 03 14:29:00 CST 2011</requestTime> </header2> </soapenv:Header> <soapenv:Body> <ns2:getUserProfile xmlns:ns2="http://axis.test.com/ws" /> </soapenv:Body> </soapenv:Envelope

 

 

第三种:SOAPHeaderBlock

 

       ---  on stub

 

       OMNamespace omNamespace = OMAbstractFactory.getOMFactory() .createOMNamespace("http://myCompany.com", "headerNs"); SOAPHeaderBlock header1 = OMAbstractFactory.getSOAP12Factory() .createSOAPHeaderBlock("header1", omNamespace); header1.addChild(AXIOMUtil.stringToOM("<header123>header content</header123>")); _stub._getServiceClient().addHeader(header1);

 

      --- soap message

 

       <?xml version='1.0' encoding='utf-8'?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Header> <headerNs:header1 xmlns:headerNs="http://myCompany.com"> <header123>header content</header123> </headerNs:header1> </soapenv:Header> <soapenv:Body> <ns2:getUserProfile xmlns:ns2="http://axis.test.com/ws" /> </soapenv:Body> </soapenv:Envelope>

 

 

      注意,如果想在response中获得custom soap header,可参照上一篇的方式,自定义module方式处理。

 

 

 

 


 

 

你可能感兴趣的:(java,Hibernate,webservice,header,SOAP,encoding)