CXF学习(进阶)

来源:互联网 发布:java string replace 编辑:程序博客网 时间:2024/06/03 19:05
一、自定义对象传递(简单JavaBean对象)。
 第一步:创建传输的JavaBean对象(UserInfo)
[java] view plaincopy
  1. package com.ws.model;  
  2.   
  3. import javax.xml.bind.annotation.XmlAccessType;  
  4. import javax.xml.bind.annotation.XmlAccessorType;  
  5. import javax.xml.bind.annotation.XmlRootElement;  
  6. import javax.xml.bind.annotation.XmlType;  
  7.   
  8.   
  9. @XmlRootElement(name=“UserInfo”)  
  10. @XmlAccessorType(XmlAccessType.FIELD)  
  11. @XmlType(propOrder={“userName”,“userAge”})  
  12. public class UserInfo {  
  13.     private String userName;  
  14.     private Integer userAge;  
  15.       
  16.     public UserInfo(String name,Integer age){  
  17.         this.userAge = age;  
  18.         this.userName = name;  
  19.     }  
  20.       
  21.     public UserInfo(){        
  22.     }  
  23.       
  24.     // 添加geter/seter方法..  
  25.       
  26. }  

注解:@XmlRootElement-指定XML根元素名称(可选) 
            @XmlAccessorType-控制属性或方法序列化 , 四种方案: 
                       FIELD-对每个非静态,非瞬变属性JAXB工具自动绑定成XML,除非注明XmlTransient 
                       NONE-不做任何处理 
                       PROPERTY-对具有set/get方法的属性进行绑定,除非注明XmlTransient 
                       PUBLIC_MEMBER -对有set/get方法的属性或具有共公访问权限的属性进行绑定,除非注 明XmlTransient 
            @XmlType-映射一个类或一个枚举类型成一个XML Schema类型

第二步:创建webservices服务端接口和实现类 
   
 1、创建服务端接口类

[java] view plaincopy
  1. package com.ws.services;  
  2.   
  3. import javax.jws.WebService;  
  4. import com.ws.model.UserInfo;  
  5.   
  6. @WebService  
  7. public interface IUserServices {  
  8.     public UserInfo getUserInfo(String userName, Integer userAge);  
  9. }  

   2、创建服务端接口实现类

[java] view plaincopy
  1. package com.ws.services.impl;  
  2.   
  3. import javax.jws.WebService;  
  4. import com.ws.model.UserInfo;  
  5. import com.ws.services.IUserServices;  
  6.   
  7. @WebService  
  8. public class UserServicesImpl implements IUserServices {  
  9.     public UserInfo getUserInfo(String userName, Integer userAge) {  
  10.         return new UserInfo(userName,userAge);  
  11.     }  
  12. }  

3、创建服务端,并发布服务

[java] view plaincopy
  1. package com.test;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  5. import com.ws.services.impl.UserServicesImpl;  
  6.   
  7. public class ServerTest {  
  8.     public ServerTest(){  
  9.         // 发布User服务接口  
  10.         Endpoint.publish(”http://localhost:8090/userInfoServices”new UserServicesImpl());  
  11.     }  
  12.     public static void main(String[] args) {  
  13.         // 启动服务  
  14.         new ServerTest();  
  15.         System.out.println(”Server ready…”);     
  16.         try {  
  17.             Thread.sleep(1000*300);//休眠五分分钟,便于测试     
  18.         } catch (InterruptedException e) {  
  19.             e.printStackTrace();  
  20.         }     
  21.         System.out.println(”Server exit…”);     
  22.         System.exit(0);  
  23.     }  
  24. }  

第三步:创建webservices客户端,并进行测试(这里只例举在客户端工程中的测试方法) 
    1、将服务端的JavaBean和Services接口类,copy到客户端工程中,且目录要一致
    2、在客户端工程中,新建一个测试类测试

[java] view plaincopy
  1. package com.ws.client;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4. import com.ws.model.UserInfo;  
  5. import com.ws.server.IUserServices;  
  6.   
  7. public class UserTest {  
  8.     public static void main(String[] args) {  
  9.         //创建WebService客户端代理工厂     
  10.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();     
  11.         //注册WebService接口     
  12.         factory.setServiceClass(IUserServices.class);     
  13.         //设置WebService地址     
  14.         factory.setAddress(”http://localhost:8090/userInfoServices”);          
  15.         IUserServices userServices = (IUserServices)factory.create();     
  16.         System.out.println(”invoke userinfo webservice…”);  
  17.         // 测试返回JavaBean对象的  
  18.         UserInfo user = userServices.getUserInfo(”vicky”23);  
  19.         System.out.println(”UserName: ”+user.getUserName());  
  20.         System.out.println(”UserAge : ”+user.getUserAge());  
  21.           
  22.         System.exit(0);     
  23.     }   
  24. }  

第四步:运行webServices服务,在IE中输入http://localhost:8090/userInfoServices?wsdl,验证服务是否成功发布
第四步:运行客户端,验证。


二、复杂对象传递(List,Map)

前面讲到了JavaBean对象的传递,这一节我们就CXF框架复杂对象(List,Map)的传递进行讲解。 
第一步:创建存储复杂对象的类(因为WebServices的复杂对象的传递,一定要借助第三方对象(即自定义对象)来实现)

[java] view plaincopy
  1. package com.ws.model;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. public class Users {  
  7.     private ArrayList<UserInfo> userList;  // 不要用List类型  
  8.     private HashMap<Integer, UserInfo> userMap; // 不要用Map类型  
  9.     private UserInfo[] userArray;  
  10.     // geter/seter 方法  
  11. }  

第二步:创建WebServices的服务端接口和实现类

[java] view plaincopy
  1. package com.ws.services;  
  2.   
  3. import javax.jws.WebService;  
  4. import com.ws.model.UserInfo;  
  5. import com.ws.model.Users;  
  6.   
  7. @WebService  
  8. public interface IUserServices {  
  9.   
  10.     public Users getAllUsers();  
  11.       
  12.     public Users getUsersMap();  
  13.       
  14.     public Users getUsersArray();  
  15.   
  16. }  
[java] view plaincopy
  1. package com.ws.services.impl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import javax.jws.WebService;  
  6. import com.ws.model.UserInfo;  
  7. import com.ws.model.Users;  
  8. import com.ws.services.IUserServices;  
  9.   
  10. @WebService  
  11. public class UserServicesImpl implements IUserServices {  
  12.   
  13.     public Users getAllUsers() {  
  14.         ArrayList<UserInfo> list = new ArrayList<UserInfo>();  
  15.         list.add(new UserInfo(“vicky”,23));  
  16.         list.add(new UserInfo(“ivy”,26));  
  17.         list.add(new UserInfo(“simon”,26));  
  18.         list.add(new UserInfo(“carol”,29));  
  19.         Users users = new Users();  
  20.         users.setUserList(list);  
  21.         return users;  
  22.     }  
  23.   
  24.     public Users getUsersMap() {  
  25.         HashMap<Integer, UserInfo> map = new HashMap<Integer, UserInfo>();  
  26.         map.put(23new UserInfo(“vicky”,23));  
  27.         map.put(22new UserInfo(“caty”,22));  
  28.         map.put(24new UserInfo(“leah”,24));  
  29.         map.put(25new UserInfo(“kelly”,25));  
  30.         map.put(27new UserInfo(“ivy”,27));  
  31.         map.put(26new UserInfo(“simon”,26));  
  32.         map.put(29new UserInfo(“carol”,29));  
  33.           
  34.         Users users = new Users();  
  35.         users.setUserMap(map);  
  36.         return users;  
  37.     }  
  38.   
  39.     public Users getUsersArray() {  
  40.         UserInfo[] userInfo = new UserInfo[5];  
  41.         for(int i=0;i<5;i++){  
  42.             UserInfo info = new UserInfo();  
  43.             info.setUserAge(23+i);  
  44.             info.setUserName(”Array”+(i+1));  
  45.             userInfo[i] = info;  
  46.         }  
  47.         Users users = new Users();  
  48.         users.setUserArray(userInfo);  
  49.         return users;  
  50.     }  
  51.   
  52. }  

第三步:创建WebServices的服务端

[java] view plaincopy
  1. package com.test;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  5. import com.ws.services.impl.UserServicesImpl;  
  6.   
  7. public class ServerTest {  
  8.     public ServerTest(){  
  9.         // 发布User服务接口  
  10.         Endpoint.publish(”http://localhost:8090/userInfoServices”new UserServicesImpl());  
  11.     }  
  12.     public static void main(String[] args) {  
  13.         // 启动服务  
  14.         new ServerTest();  
  15.         System.out.println(”Server ready…”);     
  16.         try {  
  17.             Thread.sleep(1000*300);//休眠五分分钟,便于测试   
  18.         } catch (InterruptedException e) {  
  19.             e.printStackTrace();  
  20.         }     
  21.         System.out.println(”Server exit…”);     
  22.         System.exit(0);  
  23.     }  
  24. }  

第四步:创建WebServices的客户端,并测试
    1、将服务端创建的复杂对象的类和接口copy到客户工程中,且路径要与服务端一致;
    2、新建测试类进行测试

[java] view plaincopy
  1. package com.ws.client;  
  2.   
  3. import java.util.List;  
  4. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  5. import com.ws.model.UserInfo;  
  6. import com.ws.model.Users;  
  7. import com.ws.server.IUserServices;  
  8.   
  9. public class UserTest {  
  10.     public static void main(String[] args) {  
  11.         //创建WebService客户端代理工厂     
  12.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();     
  13.         //注册WebService接口     
  14.         factory.setServiceClass(IUserServices.class);     
  15.         //设置WebService地址     
  16.         factory.setAddress(”http://localhost:8090/userInfoServices”);          
  17.         IUserServices userServices = (IUserServices)factory.create();     
  18.         System.out.println(”invoke userinfo webservice…”);  
  19.         // 测试Map  
  20. //      testMap(userServices);  
  21.         // 测试List  
  22. //      testList(userServices);  
  23.         // 测试Array  
  24. //      testArray(userServices);  
  25.         System.exit(0);     
  26.     }   
  27.       
  28.     public static void testArray(IUserServices userServices){  
  29.         Users users = userServices.getUsersArray();  
  30.         if(users!=null){  
  31.             UserInfo[] array = users.getUserArray();  
  32.             for(UserInfo info:array){  
  33.                 System.out.println(”UserName: ”+info.getUserName());  
  34.                 System.out.println(”UserAge : ”+info.getUserAge());  
  35.             }  
  36.         }  
  37.     }  
  38.       
  39.     public static void testList(IUserServices userServices){  
  40.         Users users = userServices.getAllUsers();  
  41.         if(users!=null){  
  42.             List<UserInfo> list = users.getUserList();  
  43.             for(UserInfo info:list){  
  44.                 System.out.println(”UserName: ”+info.getUserName());  
  45.                 System.out.println(”UserAge : ”+info.getUserAge());  
  46.             }  
  47.         }  
  48.     }  
  49.       
  50.     public static void testMap(IUserServices userServices){  
  51.         Users users = userServices.getUsersMap();  
  52.         if(users!=null){  
  53.             UserInfo info = users.getUserMap().get(23);  
  54.             System.out.println(”UserName: ”+info.getUserName());  
  55.             System.out.println(”UserAge : ”+info.getUserAge());  
  56.         }  
  57.     }  
  58. }  

第五步:运行服务端,验证webservices服务是否发布成功
第六步:运行客户端,验证是否成功调用webservices服务

注:在做webServices复杂对象传递时,返回值的类型不要用接口类型。例如(List 应该换成ArrayList ,Map应该换成HashMap)



三、CXF整合Sring框架

 通过前面两节的讲解,相信你对CXF框架开始有一些认识了。在当今项目开发中,Spring框架基上都用到过,那么它怎么与CXF结合呢,这就是我们这一间要讲的内容。好了,闲话少说。 
    首先,在前面基础上再导入几个spring要用到的几个.jar包:
               1、spring-asm.jar
               2、spring-beans.jar
               3、spring-context.jar
               4、spring-core.jar
               5、spring-expression.jar
               6、spring-aop.jar
               7、spring-web.jar
第一步:新建一个服务端web project,导入要用到的cxf和spring的.jar包,修改web.xml。配置如下
[html] view plaincopy
  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <web-app version=“2.4”   
  3.     xmlns=“http://java.sun.com/xml/ns/j2ee”   
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”   
  5.     xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>  
  7.     <!– Spring 容器加载的配置文件 设置 –>  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>  
  11.             classpath:/applicationContext-server.xml  
  12.         </param-value>  
  13.     </context-param>  
  14.       
  15.          <!– Spring 配置 –>  
  16.          <listener>  
  17.                   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  18.          </listener>  
  19.          <listener>  
  20.                   <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  21.          </listener>  
  22.       
  23.     <!– WebServices设置 –>  
  24.     <servlet>  
  25.         <servlet-name>CXFServices</servlet-name>  
  26.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  27.         <load-on-startup>0</load-on-startup>  
  28.     </servlet>  
  29.     <servlet-mapping>  
  30.         <servlet-name>CXFServices</servlet-name>  
  31.         <url-pattern>/services/*</url-pattern>  
  32.     </servlet-mapping>  
  33.       
  34.   <welcome-file-list>  
  35.     <welcome-file>index.jsp</welcome-file>  
  36.   </welcome-file-list>  
  37. </web-app>  
第二步:新建一个接口类和接口实现类
[java] view plaincopy
  1. package com.ms.services;  
  2.   
  3. import java.util.List;  
  4. import javax.jws.WebService;  
  5. import com.ms.model.UserInfo;  
  6.   
  7. @WebService  
  8. public interface IHelloServices {  
  9.     public String sayHello(String name);  
  10.     public String sayHelloToAll(List<UserInfo> users);  
  11. }  
[java] view plaincopy
  1. package com.ms.services.impl;  
  2.   
  3. import java.util.List;  
  4. import javax.jws.WebService;  
  5. import com.ms.model.UserInfo;  
  6. import com.ms.services.IHelloServices;  
  7.   
  8. @WebService(endpointInterface=“com.ms.services.IHelloServices”)  
  9. public class HelloServicesImpl implements IHelloServices {  
  10.   
  11.     public String sayHello(String name) {  
  12.         return “Hello ”+name+“ .”;  
  13.     }  
  14.     public String sayHelloToAll(List<UserInfo> users) {  
  15.         String hello = ”hello ”;  
  16.         for(UserInfo user:users){  
  17.             hello += user.getUserName()+” ,”;  
  18.         }  
  19.         hello += ” ,everybody.”;  
  20.         return hello;  
  21.     }  
  22. }  
第三步:新建一个spring Bean的xml文件,配置CXF webservices的服务
[html] view plaincopy
  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <beans  xmlns=“http://www.springframework.org/schema/beans”  
  3.         xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  4.         xmlns:jaxws=“http://cxf.apache.org/jaxws”  
  5.         xsi:schemaLocation=”    
  6.             http://www.springframework.org/schema/beans     
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.             http://cxf.apache.org/jaxws    
  9.             http://cxf.apache.org/schemas/jaxws.xsd”>  
  10.     <!– Import apache CXF bean definition 固定–>  
  11.     <import resource=“classpath:META-INF/cxf/cxf.xml” />  
  12.     <import resource=“classpath:META-INF/cxf/cxf-extension-soap.xml” />  
  13.     <import resource=“classpath:META-INF/cxf/cxf-servlet.xml” />  
  14.       
  15.     <!– services接口配置 –>  
  16.     <bean id=“helloServicesBean” class=“com.ms.services.impl.HelloServicesImpl” />  
  17.     <!– CXF 配置WebServices的服务名及访问地址 –>  
  18.     <jaxws:server id=“helloServices” address=“/HelloServices”   
  19.             serviceClass=“com.ms.services.IHelloServices”>  
  20.             <jaxws:serviceBean>  
  21.                 <ref bean=“helloServicesBean”/>  
  22.             </jaxws:serviceBean>  
  23.     </jaxws:server>  
  24. </beans>  
第四步:将工程部署到Tomcat中运行,在IE中输入”http://localhost:8090/CxfServer_Spring/services”,测试服务是否发布成功
第五步:新建一个客户端web project,导入要用到的cxf和spring的.jar包
第六步:将服务端的接口类及JavaBean对象类copy到客户端工程中,且路径要与服务端路径一致
第七步:新建一个spring Bean的xml配置文件,配置CXF webservices的客户端
[html] view plaincopy
  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <beans  xmlns=“http://www.springframework.org/schema/beans”  
  3.         xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  4.         xmlns:jaxws=“http://cxf.apache.org/jaxws”  
  5.         xsi:schemaLocation=”    
  6.             http://www.springframework.org/schema/beans     
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.             http://cxf.apache.org/jaxws    
  9.             http://cxf.apache.org/schemas/jaxws.xsd”>  
  10.     <!– Import apache CXF bean definition –>  
  11.     <import resource=“classpath:META-INF/cxf/cxf.xml” />  
  12.     <import resource=“classpath:META-INF/cxf/cxf-extension-soap.xml” />  
  13.     <import resource=“classpath:META-INF/cxf/cxf-servlet.xml” />  
  14.       
  15.     <!– CXF webservices 客户端配置 –>  
  16.     <jaxws:client id=“helloClient” serviceClass=“com.ms.services.IHelloServices”   
  17.             address=“http://localhost:8090/CxfServer_Spring/services/HelloServices”>  
  18.     </jaxws:client>  
  19. </beans>  
第八步:新建一个测试类进行测试,代码如下
[java] view plaincopy
  1. package com.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8. import com.ms.model.UserInfo;  
  9. import com.ms.services.IHelloServices;  
  10.   
  11. public class Client {  
  12.     public static void main(String[] args) {  
  13.         invokeBySpring();  
  14.     }  
  15.       
  16.     /** 
  17.      * 通过Spring测试webservices 
  18.      */  
  19.     public static void invokeBySpring(){  
  20.         ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext-client.xml”);  
  21.         IHelloServices helloServices = context.getBean(”helloClient”,IHelloServices.class);  
  22.           
  23.         List<UserInfo> users = new ArrayList<UserInfo>();  
  24.         users.add(new UserInfo(“vicky”,23));  
  25.         users.add(new UserInfo(“caty”,23));  
  26.         users.add(new UserInfo(“ivy”,23));  
  27.         users.add(new UserInfo(“kelly”,23));  
  28.         String helloAll = helloServices.sayHelloToAll(users);  
  29.           
  30.         System.out.println(helloAll);  
  31.     }  
  32.       
  33.     public static void invoke(){  
  34.         //创建WebService客户端代理工厂     
  35.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();     
  36.         //注册WebService接口     
  37.         factory.setServiceClass(IHelloServices.class);     
  38.         //设置WebService地址     
  39.         factory.setAddress(”http://localhost:8090/CxfServer_Spring/services/HelloServices”);          
  40.         IHelloServices helloServices = (IHelloServices)factory.create();     
  41.         System.out.println(”invoke helloServices webservice…”);  
  42.         String hello = helloServices.sayHello(”vicky”);  
  43.           
  44.         System.out.println(hello);  
  45.     }  
  46. }  
0 0
原创粉丝点击