分享

xfire+spring配置webservice实例讲解

 学学习无止境 2016-09-26
web.xml: 
Java代码  收藏代码
  1. <!-- begin xfire -->  
  2.     <servlet>  
  3.        <!-- 配合Spring容器中XFire一起工作的Servlet-->  
  4.        <servlet-name>xfireServlet</servlet-name>  
  5.        <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>  
  6.     </servlet>  
  7.     <servlet-mapping>  
  8.        <servlet-name>xfireServlet</servlet-name>  
  9.        <!-- 在这个URI下开放Web Service服务 -->  
  10.        <url-pattern>/service/*</url-pattern>  
  11.     </servlet-mapping>   
  12. <!-- end xire -->  


xfire-servlet.xml 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
  3.     "http://www./dtd/spring-beans.dtd">  
  4.   
  5. <beans>  
  6.   
  7.     <!-- 引入XFire预配置信息 -->  
  8.     <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />  
  9.   
  10.     <!-- Web服务实现类,就是要发布成web服务的pojo,标注了@WebService注解  -->    
  11.     <bean id="userService" class="com.test.xfire.UserServiceImpl" />  
  12.   
  13.   
  14.     <!-- 获得applicationContext中所有bean的JSR181 annotation -->   
  15.     <!-- 该Bean获取Spring容器中所有标注@WebService的Bean -->  
  16.     <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />   
  17.       
  18.     <!-- 对标注@WebService的Bean进行处理,完成导出工作  -->  
  19.     <bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">   
  20.         <property name="xfire" ref="xfire" />   
  21.         <property name="webAnnotations" ref="webAnnotations" />   
  22.     </bean>  
  23.   
  24. </beans>  


Java代码  收藏代码
  1. package com.test.xfire;  
  2.   
  3. public class User  
  4. {  
  5.     private String username;  
  6.     private int age;  
  7.     private String hobby;  
  8.   
  9.     public String getUsername()  
  10.     {  
  11.         return username;  
  12.     }  
  13.   
  14.     public void setUsername(String username)  
  15.     {  
  16.         this.username = username;  
  17.     }  
  18.   
  19.     public int getAge()  
  20.     {  
  21.         return age;  
  22.     }  
  23.   
  24.     public void setAge(int age)  
  25.     {  
  26.         this.age = age;  
  27.     }  
  28.   
  29.     public String getHobby()  
  30.     {  
  31.         return hobby;  
  32.     }  
  33.   
  34.     public void setHobby(String hobby)  
  35.     {  
  36.         this.hobby = hobby;  
  37.     }  
  38.   
  39.     public String toString()  
  40.     {  
  41.         return "用户:" + username + ",年龄" + age + "时,爱好:" + hobby;  
  42.     }  
  43. }  



Java代码  收藏代码
  1. package com.test.xfire;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. @WebService  
  6. public interface IUserService  
  7. {  
  8.     public User findUserHobby(User user) throws Exception;  
  9. }  


Java代码  收藏代码
  1. package com.test.xfire;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.jws.WebService;  
  7.   
  8. /** 
  9.  * serviceName是这这个服务的名称,默认为接口实现类的名称,endpointInterface是该类实现的接口的类全名<BR> 
  10.  * 访问:http://localhost:8080/ssh/service/userServiceImpl?wsdl 
  11.  *  
  12.  * @author Administrator 
  13.  */  
  14. @WebService(serviceName = "userServiceImpl", endpointInterface = "com.test.xfire.IUserService")  
  15. public class UserServiceImpl implements IUserService  
  16. {  
  17.     private static final Map<String, String> mapUser = new HashMap<String, String>();  
  18.   
  19.     static  
  20.     {  
  21.         mapUser.put("jg.sun""篮球");  
  22.         mapUser.put("lcrystal""足球");  
  23.         mapUser.put("s0meb0dy""游泳");  
  24.         mapUser.put("猫来猫去""睡觉");  
  25.         mapUser.put("小刚""唱歌");  
  26.     }  
  27.   
  28.     public User findUserHobby(User user) throws Exception  
  29.     {  
  30.         if (user == null)  
  31.         {  
  32.             return null;  
  33.         }  
  34.   
  35.         String hobby = mapUser.get(user.getUsername());  
  36.   
  37.         if (hobby == null)  
  38.         {  
  39.             user.setHobby("无");  
  40.         }  
  41.         else  
  42.         {  
  43.             user.setHobby(hobby);  
  44.         }  
  45.         return user;  
  46.     }  
  47. }  


客户端调用2种方式: 
1、通过WSDL文件生成客户端调用程序,先在目录存放WSDL文件 
Java代码  收藏代码
  1. package com.test.xfire.client;  
  2.   
  3. import org.codehaus.xfire.client.Client;  
  4. import org.springframework.core.io.ClassPathResource;  
  5. import org.springframework.core.io.Resource;  
  6.   
  7. import com.test.xfire.IUserService;  
  8. import com.test.xfire.User;  
  9.   
  10. /** 
  11.  * 通过WSDL文件生成客户端调用程序 
  12.  *  
  13.  * @author Administrator 
  14.  */  
  15. public class WebServiceClientTestByWsdl  
  16. {  
  17.     IUserService iUserService = null;  
  18.   
  19.     public static void main(String[] args) throws Exception  
  20.     {  
  21.         WebServiceClientTest test = new WebServiceClientTest();  
  22.         test.testClient();  
  23.     }  
  24.   
  25.     public void testClient() throws Exception  
  26.     {  
  27.         String wsdl = "userServiceImpl.wsdl"// 对应的WSDL文件  
  28.         Resource resource = new ClassPathResource(wsdl);  
  29.         Client client = new Client(resource.getInputStream(), null); // 根据WSDL创建客户实例  
  30.   
  31.         User user = new User();  
  32.         user.setUsername("猫来猫去");  
  33.         Object[] objArray = new Object[1];  
  34.         objArray[0] = user;  
  35.         // 调用特定的Web Service方法  
  36.         Object[] results = client.invoke("findUserHobby", objArray);  
  37.         System.out.println("result: " + results[0]);  
  38.     }  
  39. }  



2、根据服务地址创建客户端调用程序 

webserviceClient.xml 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="GBK"?>  
  2.   
  3. <beans xmlns="http://www./schema/beans"  
  4.     xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context"  
  5.     xmlns:tx="http://www./schema/tx" xmlns:aop="http://www./schema/aop"  
  6.     xsi:schemaLocation="http://www./schema/beans   
  7.     http://www./schema/beans/spring-beans-2.5.xsd  
  8.     http://www./schema/context   
  9.     http://www./schema/context/spring-context-2.5.xsd  
  10.     http://www./schema/tx   
  11.     http://www./schema/tx/spring-tx-2.5.xsd   
  12.     http://www./schema/aop   
  13.     http://www./schema/aop/spring-aop-2.5.xsd">  
  14.   
  15.     <bean  id ="testWebService"  class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">  
  16.         <property name ="serviceClass">            
  17.             <value>com.test.xfire.IUserService</value>         
  18.         </property>        
  19.         <property name ="wsdlDocumentUrl">           
  20.             <value>http://localhost:8080/ssh/service/userServiceImpl?wsdl</value>         
  21.         </property>        
  22.     </bean>  
  23.   
  24.       
  25. </beans>  


Java代码  收藏代码
  1. package com.test.xfire.client;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.test.xfire.IUserService;  
  7. import com.test.xfire.User;  
  8.   
  9. /** 
  10.  * 根据服务地址创建客户端调用程序 
  11.  *  
  12.  * @author Administrator 
  13.  */  
  14. public class WebServiceClientTest  
  15. {  
  16.     IUserService iUserService = null;  
  17.   
  18.     public static void main(String[] args)  
  19.     {  
  20.         WebServiceClientTest test = new WebServiceClientTest();  
  21.         test.testClient();  
  22.     }  
  23.   
  24.     public void testClient()  
  25.     {  
  26.         ApplicationContext ctx = new ClassPathXmlApplicationContext("webserviceClient.xml");  
  27.         iUserService = (IUserService) ctx.getBean("testWebService");  
  28.         try  
  29.         {  
  30.             User user = new User();  
  31.             user.setUsername("猫来猫去");  
  32.             System.out.println(iUserService.findUserHobby(user));  
  33.         }  
  34.         catch (Exception e)  
  35.         {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多