微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实现WebServices二:使用XFireSpringServlet与ServiceBean


使用XFire进行web services开发有三种方式:

1. 不集成Spring: 配置services.xml 文件和web.xml(配置org.codehaus.xfire.transport.http.XFireConfigurableServlet servlet)

2. Spring使用XFireSpringServlet方式。

3.Spring集成使用org.springframework.web.servlet.dispatcherServlet方式


使用XFireSpringServlet进行实现的步骤:

1、定义服务接口;

2、实现服务接口;

3、更新web.xml添加Spring监听器相关XFire相关的servlet(即添加XFireSpringServlet)和Spring监听器;

4、在Spring文件application.xml配置ServiceBean;

5、测试web services是否部署成功


1、定义服务接口

package com.mybank.xfire.example;


public interface IBankingService {

     public String transferFunds(  

          String fromAccount,String toAccount,double amount,String currency);  

}

注:服务接口不能少,客户端访问web services就是通过接口进行访问的,实现类对客户端是透明的。


2、实现服务接口

import java.text.NumberFormat; i

import java.text.DecimalFormat;

public class BankingService implements IBankingService {  

    public BankingService(){      

    }  

    public String transferFunds(  

        String fromAccount,String currency){  

        String statusMessage = "";  

        try {  

            NumberFormat formatter = new DecimalFormat("###,###,###.00");        

            statusMessage = "COMPLETED: " + currency + " " + formatter.format(amount)+  

            " was successfully transferred from A/C# " + fromAccount + " to A/C# " + toAccount;  

        } catch (Exception e){  

            statusMessage = "BankingService.transferFunds(): EXCEPTION: " + e.toString();  

        }  

        return statusMessage;  

    }

}


注:BankingService的transferFunds返回类型是String,如果是复杂类型需要Aegis进行数据绑定。


3、配置web.xml

<web-app>

<display-name>Spring Image Database</display-name>

<description>Spring Image Database sample application</description>

<!--配置spring上下文文件路径-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath:applicationContext.xml

</param-value>

</context-param>

<!--配置log4j日志监听器-->

<listener>

<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</listener>

<!-- 配置spring上下文加载监听器 -->

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

<servlet>

<!--配置处理XFire处理web服务请求的servlet-->

<servlet-name>XFireServlet</servlet-name>

<display-name>XFire Servlet</display-name>

<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>

</servlet>

<servlet-mapping>

<url-pattern>/services/*</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

注:使用Spring管理XFire bean就应该配置Spring监听器和spring上下文配置文件路径。


4、在Spring上下文配置文件application.xml中配置XFire ServiceBean

<beans>

   <!--引入xfire.xml 这个文件包含了定义TransportManager,ServiceRegistry,和一些简单的ServiceFactorys的bean-->

   <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

   <bean name="BankService" class="org.codehaus.xfire.spring.ServiceBean">

      <property name="serviceBean" ref="bank"/>

      <property name="serviceClass" value="com.mybank.xfire.example.IBankingService"/>

      <property name="inHandlers">

         <list>

            <ref bean="addressingHandler"/>

         </list>

      </property>

   </bean>

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

   <bean id="bank" class="com.mybank.xfire.example.BankingService"/>

</beans>

 


5、测试,略

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐