Webservice服務開發之XFire+Spring整合(使用DispatcherServlet方式)

1、XFire概述java

  XFire是codeHaus組織提供的一個開源的Web Service框架,它構建了普通Java對象和服務架構之間的橋樑,支持將普通Java對象經過很是簡單的方式發佈成Web服務,這種處理方式大大簡化了Java應用轉化爲Web服務的步驟和過程,也直接下降了服務架構的實現難度。雖然XFire在2007年後已中止更新(目前XFire框架最後一個版本爲1.2.6),但因爲它能夠很輕易地與Spring集成,使得其在Java企業級開發中仍有着一席之地。本文將簡單地介紹使用Spring框架中自帶的DispatcherServlet來整合XFire開發Webservice服務。web

2、開發環境及工具spring

  Eclipse+Tomcat 7+JDK 1.7+SoapUI(Webservice測試工具)數據庫

3、整合案例源碼瀏覽器

  該整合案例是入門級別的,並未使用到數據庫,提供webservice的服務接口類也僅是簡單的幾句話而已。經過簡化代碼來突出主題是本人的一向宗旨,望諒解。架構

一、所使用的Jar包app

二、提供webservice服務的接口類框架

package com.tiger.xfire.service;
/**
 * xfire服務端測試接口
 */
public interface XfireTestService {
    public String getRespStr(String reqstr);
}

三、提供webservice服務的接口實現類jsp

package com.tiger.xfire.service.impl;

import com.tiger.xfire.service.XfireTestService;
/**
 * xfire服務端接口對應的實現類
 */
public class XfireTestServiceImpl implements XfireTestService{
    
    @Override
    public String getRespStr(String reqStr) {
        return "測試信息已收到!";
    }
}

四、web.xml文件配置ide

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>xfire-demo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 引入 spring配置文件和xfire自帶spring集成文件xfire.xml-->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml,classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
  </context-param>
 
  <!-- 註冊spring監聽 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 防止內存泄漏(spring框架自帶監聽) -->
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  
  <!-- 使用DispatcherServlet方式集成xfire-->
  <servlet>
      <!-- 此處的servlet-name的值與xfire-servlet.xml中的xfire對應 -->
    <servlet-name>xfire</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>xfire</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>

五、applicationContext.xml配置(本案例中該文件放在src目錄下)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!-- 數據源、事務、切面等配置此處忽略 -->
    
    <!-- 將提供ws的接口交給spring容器管理 -->
    <bean id="xfireTestService" class="com.tiger.xfire.service.impl.XfireTestServiceImpl" autowire="byType"/>
</beans>

六、xfire-servlet.xml配置(放在WEB-INF目錄下)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <!-- 配置訪問ws服務的地址-->
                <entry key="/xfiretest">
                    <!-- 指定須要調用的服務 -->
                    <ref bean="xfiretest" />
                </entry>
            </map>
        </property>
    </bean>
    
    <!-- 配置ws服務須要用到的公共bean信息 -->
    <bean id="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">
        <!-- 配置ws服務使用的工廠 -->
        <property name="serviceFactory">
            <!-- 引用xfire框架中org/codehaus/xfire/spring/xfire.xml文件的默認工廠配置-->
            <ref bean="xfire.serviceFactory" />
        </property>
        <property name="xfire">
            <ref bean="xfire" />
        </property>
    </bean>
    
    <!-- 配置ws服務 -->
    <bean id="xfiretest" parent="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <!-- 指定提供ws服務的實現類(對應spring配置文件中配置的bean)-->
        <property name="serviceBean">
            <ref bean="xfireTestService" />
        </property>
        <!-- 指定提供ws服務的接口 -->
        <property name="serviceClass">
            <value>com.tiger.xfire.service.XfireTestService</value>
        </property>
        <!-- 指定返回報文的命名空間 -->
        <property name="namespace">
            <value>http://com.tiger.service</value>
        </property>
    </bean>
    
</beans>

4、案例測試

一、在瀏覽器中直接輸入http://localhost:8080/xfire-demo/services/xfiretest?wsdl進行訪問,若出現以下內容則表明Webservice服務成功發佈:

<wsdl:definitions xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://com.tiger.service" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"
    xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding"
    targetNamespace="http://com.tiger.service">
    <wsdl:types>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            attributeFormDefault="qualified" elementFormDefault="qualified"
            targetNamespace="http://com.tiger.service">
            <xsd:element name="getRespStr">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element maxOccurs="1" minOccurs="1" name="in0"
                            nillable="true" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="getRespStrResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element maxOccurs="1" minOccurs="1" name="out"
                            nillable="true" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="getRespStrResponse">
        <wsdl:part name="parameters" element="tns:getRespStrResponse"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="getRespStrRequest">
        <wsdl:part name="parameters" element="tns:getRespStr"></wsdl:part>
    </wsdl:message>
    <wsdl:portType name="XfireTestServicePortType">
        <wsdl:operation name="getRespStr">
            <wsdl:input name="getRespStrRequest" message="tns:getRespStrRequest"></wsdl:input>
            <wsdl:output name="getRespStrResponse" message="tns:getRespStrResponse"></wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="XfireTestServiceHttpBinding" type="tns:XfireTestServicePortType">
        <wsdlsoap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="getRespStr">
            <wsdlsoap:operation soapAction="" />
            <wsdl:input name="getRespStrRequest">
                <wsdlsoap:body use="literal" />
            </wsdl:input>
            <wsdl:output name="getRespStrResponse">
                <wsdlsoap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="XfireTestService">
        <wsdl:port name="XfireTestServiceHttpPort" binding="tns:XfireTestServiceHttpBinding">
            <wsdlsoap:address
                location="http://localhost:8080/xfire-demo/services/xfiretest" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

二、使用SoapUI進行測試

  關於SoapUI的下載地址及用法本文不作介紹,感興趣的能夠百度查找相關教程,或者本身寫個Webservice客戶端進行驗證。

  上圖用紅框標記的內容是須要進行覈對的內容,若是結果差很少則證實發布的Webservice服務是能夠調通且能正常返回的。

相關文章
相關標籤/搜索