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



使用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是否部署成功


参考searcher项目中applicationContext_webservice.xml中的配置




1、定义服务接口


package com.mybank.xfire.example;






public interface IBankingService {


     public String transferFunds(  


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


}


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






2、实现服务接口


package com.mybank.xfire.example;






import java.text.NumberFormat; i


import java.text.DecimalFormat;






public class BankingService implements IBankingService {  






    public BankingService(){      


    }  






    public String transferFunds(  


        String fromAccount, String toAccount, double amount, 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>


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


</listener>


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


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


<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、客户端测试:
 首先把服务器端的打成jar包
新建一个web动态工程,添加包,
创建一个测试类:
Xml代码  
import java.net.MalformedURLException;   
import java.rmi.RemoteException;  
import org.codehaus.xfire.client.XFireProxyFactory;   
import org.codehaus.xfire.service.Service;   
import org.codehaus.xfire.client.*;   
import org.codehaus.xfire.service.binding.ObjectServiceFactory;   
import com.zx.xfiretext.domain.User;   
import com.zx.xfiretext.webserver.UserService;   
public class MyClient   
{   
    public static void main(String[] args)   
    {  
try {  
    Service serviceModel = new ObjectServiceFactory() .create(UserService.class);   
    UserService service = (UserService) new XFireProxyFactory().create( serviceModel, "http://localhost:8080/XFireService/services/UserService");   
    User user = service.queryUserByAccoutId("123");   
    System.out .println("userId=" + user.getUserId() + ", userName=" + user.getUserName() + ", lastLogin=" + user.getLastLogin());   
    }  
catch (MalformedURLException e) {  
    e.printStackTrace();   
    }  
}   
    }   
 
 运行测试类,如果在控制台输入的是
userId=123, userName=测试用户, lastLogin=Thu May 22 11:26:48 CST 2008
则说明测试成功。
Ps:如果出现javax.xml.transform.TransformerFactoryConfigurationError: Provider org.apache.xalan.processor.TransformerFactoryImpl not found错误,
错误原因是: 
认为是由于jdk1.5 与 tomcat5.0之间的关于 TransformerFactoryImpl 类的冲突造成的。
tomcat-5.0.28\common\endorsed下有两个jar包:xercesImpl.jar和xml-apis.jar,其中的类 javax.xml.transform.TransformerFactory 与jdk1.5中的类org.apache.xalan.processor.TransformerFactoryImpl其实是同一个类。
in tomcat java is called with the following argument:
-Djava.endorsed.dirs="X:\my_app\Portal\tomcat\common\endorsed"
In this directory you find two jar files: xercesImpl.jar and xml-apis.jar needed by tomcat and that must be loaded before all xmsl stuff present in the jdk (1.4 naming problem). And in the file xml-apis.jar the TransformerFactoryImpl is set to "org.apache.xalan.processor.TransformerFactoryImpl".


解决办法:
1. 将xml-apis.jar移出endorsed文件夹。
2. 用xalan系列jar包替换原来的xercesImpl.jar和xml-apis.jar。
xalan系列jar包:serializer.jar、xalan.jar、xercesImpl.jar和xml-apis.jar。
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Web Services 是 Spring 框架中的一个模块,它提供了基于 SOAP 和 WSDL 的 Web 服务开发框架。使用 Spring Web Services,开发人员可以轻松地创建和发布 Web 服务,同时也可以使用现有的 WSDL 和 XSD 文件来生成 Web 服务端点和客户端代码。 Spring Web Services 的主要特性包括: 1. 基于 Spring 框架和 Spring MVC 的 Web 服务开发框架。 2. 支持 SOAP 和 WSDL 规范,可以通过 WSDL 和 XSD 文件来生成 Web 服务端点和客户端代码。 3. 支持多种 Web 服务协议,包括 SOAP 1.1、SOAP 1.2、XML-RPC 和 HTTP GET/POST。 4. 支持多种消息格式,包括 XML、JSON 和二进制。 5. 支持 Web 服务安全,包括基于 SOAP 的安全性和 HTTPS 等协议级别的安全性。 使用 Spring Web Services 进行 Web 服务开发的过程如下: 1. 定义 Web 服务的接口和数据模型,可以使用 JAXB、XMLBeans 或 Codehaus XFire 等工具来生成 Java 类。 2. 定义 Web 服务的 WSDL 和 XSD 文件,可以使用 Eclipse WTP、Apache Axis 或 Codehaus XFire 等工具来生成。 3. 创建 Spring Web Services 应用程序,配置 Web 服务端点、数据源、事务管理器等组件。 4. 在 Web 服务端点中实现 Web 服务接口,包括请求处理和响应处理等逻辑。 5. 部署和发布 Web 服务,可以使用 Tomcat、Jetty 或 JBoss 等 Web 容器。 需要注意的是,在使用 Spring Web Services 进行 Web 服务开发时,需要考虑 Web 服务的安全性、性能和可扩展性等问题。同时,也需要遵循 SOAP 和 WSDL 规范,保证 Web 服务的互操作性和兼容性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值