XFire最佳實踐

前言:
XFire是新一代WebService框架,同時也支持與Spring集成,幫助咱們方便快速地在Spring框架中開發WebService應用。html

本節主要介紹XFire+Spring集成的2種經常使用的方法來實現簡單的WebService應用
一、使用XFire的XFireSpringServlet和ServiceBean
二、使用Spring的DispatcherServlet與XFire的XFireExporterjava

準備工做:
XFire官方網站下載地址:http://xfire.codehaus.org/Downloadweb

開發環境:
Window7 + Eclipse3.3 + Tomcat6 + JDK-1.6spring

XFire服務端和客戶端工程預覽圖:tomcat

 

 

First-使用XFire的XFireSpringServlet和ServiceBeanapp

1、Server-服務端實現步驟: 1.建立service接口->2.建立Service接口的實現類->3.web.xml配置(XFireSpringServlet)->4.配置ServiceBean->5.服務發佈
框架

一、建立service接口
eclipse

package webjar.foo;
import common.MyPojo;

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

 

二、建立Service接口的實現類jsp

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;
    }
}

 

三、在web.xml文件中進行XFire攔截配置(XFireSpringServlet)maven

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

 

四、新增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>

 

五、驗證服務發佈狀態

啓動tomcat正常,由於本人的web工程名稱爲XFireFoo,所以在Browser地址輸入http://ip:port/XFireFoo/remoting/MyXFireSpringServlet?wsdl

若是顯示效果和下面截圖信息一致,即說明webservice服務端搭建成功!

 


2、Client-客戶端實現步驟:

A、客戶端與WebService服務端在同一應用

一、測試樁示例代碼

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

 

二、測試結果 

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

1、Server-服務端實現步驟1.建立service接口->2.建立Service接口的實現類->3.web.xml配置(DispatcherServlet)->4.配置XFireExporter->5.服務發佈

一、建立service接口

package webjar.foo;

import common.MyPojo;

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

 

二、建立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;
    }
}

 

三、在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>

 

四、新增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>

 

五、驗證服務發佈狀態

啓動tomcat正常,由於本人的web工程名稱爲XFireFoo,所以在Browser地址輸入http://ip:port/XFireFoo/services/IWebJarService?wsdl

若是顯示效果和下面截圖信息一致,即說明webservice服務端搭建成功!

 

2、Client-客戶端實現步驟:

A、客戶端與WebService服務端在同一應用

一、本地客戶端測試樁 

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));
    }
}

 

二、測試結果

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

相關文章
相關標籤/搜索