Webservice服务开发之XFire+Spring整合(使用DispatcherServlet方式)

一、XFire概述

  XFire是codeHaus组织提供的一个开源的Web Service框架,它构建了普通Java对象和服务架构之间的桥梁,支持将普通Java对象通过非常简单的方式发布成Web服务,这种处理方式大大简化了Java应用转化为Web服务的步骤和过程,也直接降低了服务架构的实现难度。虽然XFire在2007年后已停止更新(目前XFire框架最后一个版本为1.2.6),但由于它可以很轻易地与Spring集成,使得其在Java企业级开发中仍有着一席之地。本文将简单地介绍使用Spring框架中自带的DispatcherServlet来整合XFire开发Webservice服务。

二、开发环境及工具

  Eclipse+Tomcat 7+JDK 1.7+SoapUI(Webservice测试工具)

三、整合案例源码

  该整合案例是入门级别的,并未使用到数据库,提供webservice的服务接口类也仅是简单的几句话而已。通过简化代码来突出主题是本人的一贯宗旨,望谅解。

1、所使用的Jar包

2、提供webservice服务的接口类

package com.tiger.xfire.service;
/**
 * xfire服务端测试接口
 */
public interface XfireTestService {
    public String getRespStr(String reqstr);
}

3、提供webservice服务的接口实现类

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 "测试信息已收到!";
    }
}

4、web.xml文件配置

<?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>

5、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>

6、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>

四、案例测试

1、在浏览器中直接输入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>

2、使用SoapUI进行测试

  关于SoapUI的下载地址及用法本文不做介绍,感兴趣的可以百度查找相关教程,或者自己写个Webservice客户端进行验证。

  上图用红框标记的内容是需要进行核对的内容,如果结果差不多则证明发布的Webservice服务是可以调通且能正常返回的。

posted @ 2018-01-31 16:37  雷德斯の边城  阅读(384)  评论(0编辑  收藏  举报