博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

XFire最佳实践

Posted on 2016-03-08 23:35  Co7Co7  阅读(455)  评论(0编辑  收藏  举报

前言:
XFire是新一代WebService框架,同时也支持与Spring集成,帮助我们方便快速地在Spring框架中开发WebService应用。

本节主要介绍XFire+Spring集成的2种常用的方法来实现简单的WebService应用
1、使用XFire的XFireSpringServlet和ServiceBean
2、使用Spring的DispatcherServlet与XFire的XFireExporter

准备工作:
XFire官方网站下载地址:http://xfire.codehaus.org/Download

开发环境:
Window7 + Eclipse3.3 + Tomcat6 + JDK-1.6

XFire服务端和客户端工程预览图:

 

 

First-使用XFire的XFireSpringServlet和ServiceBean

一、Server-服务端实现步骤: 1.创建service接口->2.创建Service接口的实现类->3.web.xml配置(XFireSpringServlet)->4.配置ServiceBean->5.服务发布

1、创建service接口

package webjar.foo;
import common.MyPojo;

public interface MyDispatcherServletXFire
{
    String divide(int dividend, int divisor);
    
    MyPojo getMyPojo(MyPojo pojo);
}

 

2、创建Service接口的实现类

package webjar.foo;
import common.MyPojo;

// org.springframework.web.servlet.DispatcherServlet与org.codehaus.xfire.spring.remoting.XFireExporter结合实现XFire
public class MyDispatcherServletXFireImpl implements MyDispatcherServletXFire
{
    @Override
    public String divide(int dividend, int divisor)
    {
        return dividend + " ÷ " + divisor + " = " + (dividend / divisor);
    }
    
    @Override
    public MyPojo getMyPojo(MyPojo pojo)
    {
        System.out.println("Hi, The client's pojo is :" + System.getProperty("line.separator") + pojo);
        
        pojo.setName("DispatcherServlet and XFireExporter to publish xfire services");
        pojo.setArray(new String[] { "Hi, Welcome to MyDispatcherServletXFire !" });
        return pojo;
    }
}

 

3、在web.xml文件中进行XFire拦截配置(XFireSpringServlet)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- Spring加载的配置文件-->
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <!-- 可以再这里加入xfire.xml也可以在applicationContext.xml中引入 -->
        <param-value>
            classpath:org/codehaus/xfire/spring/xfire.xml
            classpath:context-webservice.xml
        </param-value> 
    </context-param>

    <!-- Xfire Servlet -->
    <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <display-name>XFire Servlet</display-name>
        <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/remoting/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

4、新增context-webservice.xml文件,里面进行WebService服务的发布的基本配置(ServiceBean)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

    <!-- Spring和XFire实现方法一 -->
    <!-- org.codehaus.xfire.spring.XFireSpringServlet与org.codehaus.xfire.spring.ServiceBean结合实现XFire -->

    <!-- Service实现类-->
    <bean id="myXFireSpringServlet" class="xfirejar.foo.MyXFireSpringServletImpl" />

    <!-- 这里的name属性并不是调用时的Service名字;调用时要用类名,而不能直接使用myXFireService -->
    <bean name="myXFireService"
        class="org.codehaus.xfire.spring.ServiceBean">
        <!-- Service实现类 -->
        <property name="serviceBean" ref="myXFireSpringServlet" />
        <!-- Service接口 -->
        <property name="serviceClass" value="xfirejar.foo.MyXFireSpringServlet" />
        <property name="inHandlers">
            <list>
                <ref bean="addressingHandler" />
            </list>
        </property>
    </bean>

    <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler" />
</beans>

 

5、验证服务发布状态

启动tomcat正常,因为本人的web工程名称为XFireFoo,因此在Browser地址输入http://ip:port/XFireFoo/remoting/MyXFireSpringServlet?wsdl

如果显示效果和下面截图信息一致,即说明webservice服务端搭建成功!

 


二、Client-客户端实现步骤:

A、客户端与WebService服务端在同一应用

1、测试桩示例代码

package xfirejar.foo;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import common.MyPojo;

public class XFireJarFoo
{
    public static void main(String[] args) throws MalformedURLException
    {
        // 调用时要用类名(接口名称),而不能直接使用myXFireService
        String serviceURL = "http://localhost:8080/XFireFoo/remoting/MyXFireSpringServlet";
        Service serviceModel = new ObjectServiceFactory().create(MyXFireSpringServlet.class, null);
        XFireProxyFactory serviceFactory = new XFireProxyFactory();
        
        MyXFireSpringServlet myFire = (MyXFireSpringServlet) serviceFactory.create(serviceModel, serviceURL);
        System.out.println(myFire.divide(9, 3));
        
        MyPojo pojo = new MyPojo();
        pojo.setArray(new String[] { "remoting" });
        pojo.setName("XFireJarFoo");
        
        System.out.println(myFire.getMyPojo(pojo));
    }
}

PS:也可以使用XFire中的XFireClientFactoryBean来实现调用,可参见[转]:http://blog.csdn.net/wlbing0625/article/details/7744699

 

2、测试结果 

9 ÷ 3 = 3
MyPojo{name = XFireSpringServlet and ServiceBean to publish xfire services  array = [Hi, Welcome to MyXFireSpringServlet !]}

 

B、客户端与WebService服务端在不同应用
re: 通过访问http://ip:port/XFireFoo/remoting/MyXFireSpringServlet?wsdl地址我们可以获取到wsdl文件,
因此直接“使用eclipse自带WEB service client指定wsdl文件,从而反向生成java代码方式”进行webservice服务调用。
具体实现步骤可参见本人已发布的AXIS最佳实践章节中“客户端的开发”,So easy ! 

 

Second-使用Spring的DispatcherServlet与XFire的XFireExporter

一、Server-服务端实现步骤1.创建service接口->2.创建Service接口的实现类->3.web.xml配置(DispatcherServlet)->4.配置XFireExporter->5.服务发布

1、创建service接口

package webjar.foo;

import common.MyPojo;

public interface MyDispatcherServletXFire
{
    String divide(int dividend, int divisor);
    
    MyPojo getMyPojo(MyPojo pojo);
}

 

2、创建Service接口的实现类

package webjar.foo;

import common.MyPojo;

// org.springframework.web.servlet.DispatcherServlet与org.codehaus.xfire.spring.remoting.XFireExporter结合实现XFire
public class MyDispatcherServletXFireImpl implements MyDispatcherServletXFire
{
    @Override
    public String divide(int dividend, int divisor)
    {
        return dividend + " ÷ " + divisor + " = " + (dividend / divisor);
    }
    
    @Override
    public MyPojo getMyPojo(MyPojo pojo)
    {
        System.out.println("Hi, The client's pojo is :" + System.getProperty("line.separator") + pojo);
        
        pojo.setName("DispatcherServlet and XFireExporter to publish xfire services");
        pojo.setArray(new String[] { "Hi, Welcome to MyDispatcherServletXFire !" });
        return pojo;
    }
}

 

3、在web.xml文件中进行DispatcherServlet拦截配置(DispatcherServlet)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- Spring加载的配置文件,主要通过ContextLoader中的CONFIG_LOCATION_PARAM = "contextConfigLocation" -->
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <!-- 可以再这里加入xfire.xml也可以在applicationContext.xml中引入 -->
        <param-value>
            classpath:org/codehaus/xfire/spring/xfire.xml
            classpath:context-webservice.xml
        </param-value> 
    </context-param>
    
    <!-- Spring framework -->
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener>
    
    <!-- 注意因为servlet-name为myxfire,固xfire配置文件名应该是myxfire-servlet.xml -->  
    <servlet>    
         <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>  

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

4、新增xfire-servlet.xml文件,里面进行WebService服务的发布的基本配置(XFireExporter)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

    <!-- basic configuration -->
    <!-- 若是web.xml已经配置了org/codehaus/xfire/spring/xfire.xml,这里就不需要配置了 -->
    <!-- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> -->
    
    <!-- Service实现类-->
    <bean id="myImpl" class="webjar.foo.MyDispatcherServletXFireImpl" />

    <!-- XFire发布服务核心处理类的配置 -->
    <bean name="/IWebJarService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceFactory" ref="xfire.serviceFactory" />
        <!-- Service实现类 -->
        <property name="serviceBean" ref="myImpl" />
        <!-- Service接口 -->
        <property name="serviceClass" value="webjar.foo.MyDispatcherServletXFire" />
    </bean>
</beans>

 

5、验证服务发布状态

启动tomcat正常,因为本人的web工程名称为XFireFoo,因此在Browser地址输入http://ip:port/XFireFoo/services/IWebJarService?wsdl

如果显示效果和下面截图信息一致,即说明webservice服务端搭建成功!

 

二、Client-客户端实现步骤:

A、客户端与WebService服务端在同一应用

1、本地客户端测试桩 

package webjar.foo;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import common.MyPojo;

public class WebJarFoo
{
    public static void main(String[] args) throws MalformedURLException
    {
        // 客户端访问时使用xfire-servlet.xml中的XFireExporter配置的name属性(IWebJarService)进行调用
        String serviceURL = "http://localhost:8080/XFireFoo/services/IWebJarService";
        Service serviceModel = new ObjectServiceFactory().create(MyDispatcherServletXFire.class, null);
        XFireProxyFactory serviceFactory = new XFireProxyFactory();
        
        MyDispatcherServletXFire myFire = (MyDispatcherServletXFire) serviceFactory.create(serviceModel, serviceURL);
        System.out.println(myFire.divide(9, 3));
        
        MyPojo pojo = new MyPojo();
        pojo.setArray(new String[] { "services" });
        pojo.setName("WebJarFoo");
        
        System.out.println(myFire.getMyPojo(pojo));
    }
}

 

2、测试结果

9 ÷ 3 = 3
MyPojo{name = DispatcherServlet and XFireExporter to publish xfire services  array = [Hi, Welcome to MyDispatcherServletXFire !]}

 

B、客户端与WebService服务端在不同应用
re: 通过访问http://ip:port/XFireFoo/services/IWebJarService?wsdl地址我们可以获取到wsdl文件,
因此直接“使用eclipse自带WEB service client指定wsdl文件,从而反向生成java代码方式”进行webservice服务调用。
具体实现步骤可参见本人已发布的AXIS最佳实践章节中“客户端的开发”,So easy ! 

 

代码下载:

XFireFoo服务端和客户端示例代码:XFireFoo