1. 程式人生 > >axis2+spring整合釋出webservice服務

axis2+spring整合釋出webservice服務

1.環境搭建,準備相應環境jar包    axis2-1.6.0.0下載地址: http://axis.apache.org/axis2/java/core/download.cgi    spring相關jar包下載地址: http://maven.springframework.org/release/org/springframework/spring/ 2.建立web工程匯入axis2和spring所需jar 3.構建axis2釋出webservice服務目錄結構    /WEB-INF/services/任意名稱/META-INF/services.xml 4.配置檔案    
web.xml:   <!-- sping監聽 -->  <listener>   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>   <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/conf/applicationContext-*.xml</param-value>  </context-param>    <!-- axis2配置 -->  <servlet>      <servlet-name>Axis2Servlet</servlet-name>      <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
 </servlet>  <servlet-mapping>      <servlet-name>Axis2Servlet</servlet-name>      <url-pattern>/services/*</url-pattern>  </servlet-mapping>
    spring.xml:   <!-- 註解配置 -->  <context:annotation-config />  <context:component-scan base-package="com.axis2" />  <!-- 配置axis2交給spring管理,該配置必須存在 -->  <bean id="applicationContext"   class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />         services.xml: <?xml version="1.0" encoding="UTF-8"?> <!-- 多個servcie配置 --> <serviceGroup>  <!-- 單個service配置 -->  <!-- service名稱定義 -->  <service name="UserService">   <!-- 描述 -->   <description>    使用者資訊service   </description>   <!-- 託管給spring -->   <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier   </parameter>   <!-- spring注入的service -->   <parameter name="SpringBeanName">userService</parameter>   <!-- 接收處理器 -->   <messageReceiver    class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />  </service> </serviceGroup> 5.以上配置完成啟動tomcat,請求http://localhost:8080/axis2/services/UserService?wsdl如成功會看到wsdl檔案的定義。 6.如需處理header頭部分處理    wsdl檔案header頭部新增:     1):頭部引數描述,如下圖紅色方框內程式碼片段   2)message節點定義,Input輸入描述定義。注意對應關係     完整wsdl檔案如下,紅色部分為新增header處理:   <?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://impl.service.axis2.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://entity.axis2.com/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://impl.service.axis2.com">
    <wsdl:documentation>UserService</wsdl:documentation>
    <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://entity.axis2.com/xsd">
            <xs:complexType name="UserInfo">
                <xs:sequence>
                    <xs:element minOccurs="0" name="email" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="id" type="xs:int"/>
                    <xs:element minOccurs="0" name="password" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="phone" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="username" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
        <xs:schema xmlns:ax22="http://entity.axis2.com/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://impl.service.axis2.com">
            <xs:import namespace="http://entity.axis2.com/xsd"/>
   <xs:element name="msgHeader">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="number" type="xs:int"/>
<xs:element minOccurs="0" name="password" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>

            <xs:element name="register">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="user" nillable="true" type="ax21:UserInfo"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="registerResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" type="xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
<wsdl:message name="reqHeader">
        <wsdl:part name="header" element="ns:msgHeader"/>
    </wsdl:message>

    <wsdl:message name="registerRequest">
        <wsdl:part name="parameters" element="ns:register"/>
    </wsdl:message>
    <wsdl:message name="registerResponse">
        <wsdl:part name="parameters" element="ns:registerResponse"/>
    </wsdl:message>
    <wsdl:portType name="UserServicePortType">
        <wsdl:operation name="register">
            <wsdl:input message="ns:registerRequest" wsaw:Action="urn:register"/>
            <wsdl:output message="ns:registerResponse" wsaw:Action="urn:registerResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="UserServiceSoap11Binding" type="ns:UserServicePortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="register">
            <soap:operation soapAction="urn:register" style="document"/>
            <wsdl:input>
<soap:header use="literal" port="header" message="ns:reqHeader" wsdl:required="true"/>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="UserServiceSoap12Binding" type="ns:UserServicePortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="register">
            <soap12:operation soapAction="urn:register" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="UserServiceHttpBinding" type="ns:UserServicePortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="register">
            <http:operation location="register"/>
            <wsdl:input>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="UserService">
        <wsdl:port name="UserServiceHttpSoap11Endpoint" binding="ns:UserServiceSoap11Binding">
            <soap:address location="http://localhost:8080/axis2/services/UserService.UserServiceHttpSoap11Endpoint/"/>
        </wsdl:port>
        <wsdl:port name="UserServiceHttpSoap12Endpoint" binding="ns:UserServiceSoap12Binding">
            <soap12:address location="http://localhost:8080/axis2/services/UserService.UserServiceHttpSoap12Endpoint/"/>
        </wsdl:port>
        <wsdl:port name="UserServiceHttpEndpoint" binding="ns:UserServiceHttpBinding">
            <http:address location="http://localhost:8080/axis2/services/UserService.UserServiceHttpEndpoint/"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions> 7.服務端頭部header處理   程式碼片段:  import java.util.Iterator; import org.apache.axiom.om.impl.llom.OMElementImpl; import org.apache.axis2.context.MessageContext; import org.springframework.stereotype.Service; import com.axis2.entity.UserInfo; import com.axis2.service.UserService; @Service("userService") public class UserServiceImpl implements UserService {     @Override     public int register(UserInfo user)     {          MessageContext msgContext = MessageContext.getCurrentMessageContext();         // 獲取Head         Iterator list = (Iterator) msgContext.getEnvelope().getHeader()                 .getFirstElement().getChildren();         while (list.hasNext())         {             Object obj = list.next();             if(obj instanceof OMElementImpl){                 OMElementImpl omei = (OMElementImpl) obj;                 String nodeName = omei.getLocalName();                 String nodeValue = omei.getText();             }         }       //業務邏輯處理         return 0;     } } 至此以上步驟基本完成axis2+spring釋出webservice服務以及訊息頭處理。