xfire与spring结合的实例

xfire-servlet.xml代码  

  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   
  3. <beans>   
  4. <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>   
  5.   
  6. <bean id="userService" class="com.zx.xfiretext.webserver.UserServiceImpl"/>   
  7. <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>   
  8.   
  9. <bean name="UserService" class="org.codehaus.xfire.spring.ServiceBean">   
  10. <property name="serviceBean" ref="userServiceBean"/>   
  11. <property name="serviceClass" value="com.zx.xfiretext.webserver.UserService"/>  
  12. <property name="inHandlers">   
  13. <list>   
  14. <ref bean="addressingHandler"/> </list>   
  15. </property>  
  16. </bean>  
  17. <bean id="userServiceBean" class="com.zx.xfiretext.webserver.UserServiceImpl"/>   
  18. </beans>  

修改web.xml文件,如下

 

这样就发布了一个UserService,然后就可以进行测试:

通过ie测试:

启动tomcat,在ie中输入http://localhost:8080/XFireService/services/UserService?wsdl 出现wsdl文挡,或者输入http://localhost:8080/XFireService/services/ 出现

Available Services:

则说明配置成功,

客户端测试:

首先把服务器端的打成jar包

新建一个web动态工程,添加包,

创建一个测试类:

 

Xml代码  
  1. import java.net.MalformedURLException;   
  2. import java.rmi.RemoteException;   
  3. import org.codehaus.xfire.client.XFireProxyFactory;   
  4. import org.codehaus.xfire.service.Service;   
  5. import org.codehaus.xfire.client.*;   
  6. import org.codehaus.xfire.service.binding.ObjectServiceFactory;   
  7. import com.zx.xfiretext.domain.User;   
  8. import com.zx.xfiretext.webserver.UserService;   
  9. public class MyClient   
  10. {   
  11.      public static void main(String[] args)   
  12.      {   
  13. try {   
  14.      Service serviceModel = new ObjectServiceFactory() .create(UserService.class);   
  15.      UserService service = (UserService) new XFireProxyFactory().create( serviceModel, "http://localhost:8080/XFireService/services/UserService");   
  16.      User user = service.queryUserByAccoutId("123");   
  17.      System.out .println("userId=" + user.getUserId() + ", userName=" + user.getUserName() + ", lastLogin=" + user.getLastLogin());   
  18.      }   
  19. catch (MalformedURLException e) {   
  20.      e.printStackTrace();   
  21.      }   
  22. }   
  23.      }   

 

运行测试类,如果在控制台输入的是

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。

 

Xml代码  
  1. <?xml version="1.0" encoding="ISO-8859-1"?>   
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Spring Image Database</display-name>   
  3. <description>Spring Image Database sample application</description>   
  4. <context-param>   
  5. <param-name>contextConfigLocation</param-name>   
  6. <param-value>/WEB-INF/applicationContext.xml</param-value>   
  7. </context-param>  
  8. <listener>   
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  10. </listener>   
  11. <servlet>   
  12. <servlet-name>XFireServlet</servlet-name>   
  13. <display-name>XFire Servlet</display-name>   
  14. <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>  
  15.   </servlet>   
  16.   <servlet-mapping>   
  17.   <servlet-name>XFireServlet</servlet-name>   
  18.   <url-pattern>/services/*</url-pattern>   
  19.   </servlet-mapping>   
  20.   </web-app>   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值