axis2+spring集成发布webservice服务

时间:2022-03-15 18:23:46
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服务以及消息头处理。