【WebService】通过CXF发布WebService第一例

来源:互联网 发布:解决问题 知乎 编辑:程序博客网 时间:2024/05/17 01:08

CXF的理论知识就不重复了。

  • 发布服务

1、在OSGI bundle中发布,所以首先创建OSGI工程,并加入CXF所需要的库文件,

       MANIFEST.MF文件内容:

       

[plain] view plaincopyprint?
  1. Manifest-Version: 1.0  
  2. Bundle-ManifestVersion: 2  
  3. Bundle-Name: MySimpleWebServiceCXF01  
  4. Bundle-SymbolicName: MySimpleWebServiceCXF01  
  5. Bundle-Version: 1.0.0.qualifier  
  6. Bundle-Activator: mysimplewebservicecxf01.Activator  
  7. Bundle-ActivationPolicy: lazy  
  8. Bundle-RequiredExecutionEnvironment: JavaSE-1.6  
  9. Import-Package: org.apache.commons.logging;version="1.0.4",  
  10.  org.osgi.framework;version="1.3.0"  
  11. Bundle-ClassPath: lib/geronimo-activation_1.1_spec-1.1.jar,  
  12.  lib/geronimo-annotation_1.0_spec-1.1.1.jar,  
  13.  lib/geronimo-javamail_1.4_spec-1.7.1.jar,  
  14.  lib/geronimo-jaxws_2.2_spec-1.1.jar,  
  15.  lib/geronimo-servlet_2.5_spec-1.1.2.jar,  
  16.  lib/geronimo-stax-api_1.0_spec-1.0.1.jar,  
  17.  lib/geronimo-ws-metadata_2.0_spec-1.1.3.jar,  
  18.  lib/jaxb-api-2.2.3.jar,  
  19.  lib/jaxb-impl-2.2.4-1.jar,  
  20.  lib/jetty-util-7.5.4.v20111024.jar,  
  21.  lib/neethi-3.0.1.jar,  
  22.  lib/saaj-api-1.3.4.jar,  
  23.  lib/saaj-impl-1.3.12.jar,  
  24.  lib/wsdl4j-1.6.2.jar,  
  25.  lib/xml-resolver-1.2.jar,  
  26.  lib/xmlschema-core-2.0.1.jar,  
  27.  .,  
  28.  lib/cxf-2.5.2.jar,  
  29.  lib/org.eclipse.equinox.http.jetty_2.0.0.v20100503.jar,  
  30.  lib/jetty-server-7.5.4.v20111024.jar,  
  31.  lib/jetty-http-7.5.4.v20111024.jar,  
  32.  lib/jetty-continuation-7.5.4.v20111024.jar,  
  33.  lib/jetty-io-7.5.4.v20111024.jar,  
  34.  lib/jetty-security-7.5.4.v20111024.jar,  
  35.  lib/stax2-api-3.1.1.jar,  
  36.  lib/wstx-asl-3.2.0.jar  

  2、声明一个WebService服务接口并实现

        WebService接口文件 IUserManager.java

[java] view plaincopyprint?
  1. package mysimplewebservicecxf01.user.api;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.jws.WebService;  
  6.   
  7. import mysimplewebservicecxf01.user.api.model.User;  
  8.   
  9. @WebService  
  10. public interface IUserManager {  
  11.     int addUser(String name);  
  12.     List<User> queryUsers();  
  13.     User queryUser(int userId);  
  14.     boolean deleteUser(int userId);  
  15. }  
     Java Bean对象User.java
[java] view plaincopyprint?
  1. package mysimplewebservicecxf01.user.api.model;  
  2.   
  3. public class User {  
  4.     public int getId() {  
  5.         return id;  
  6.     }  
  7.     public void setId(int id) {  
  8.         this.id = id;  
  9.     }  
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13.     public void setName(String name) {  
  14.         this.name = name;  
  15.     }  
  16.     private int id;  
  17.     private String name;  
  18. }  

      WebService的实现 UserManager.java

[java] view plaincopyprint?
  1. package mysimplewebservicecxf01.user.impl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import mysimplewebservicecxf01.user.api.IUserManager;  
  9. import mysimplewebservicecxf01.user.api.model.User;  
  10.   
  11. public class UserManager implements IUserManager {  
  12.     Map<Integer, User> userMap = new HashMap<Integer, User>();  
  13.     int newID = 0;  
  14.     @Override  
  15.     public int addUser(String name) {  
  16.           
  17.         int newId = this.newID++;  
  18.         System.out.println("add a user begin,userId:"+newId + ",name:"+name);  
  19.         User user = new User();  
  20.         user.setId(newId);  
  21.         user.setName(name);  
  22.         userMap.put(newId, user);  
  23.         System.out.println("add a user ok");  
  24.         return newId;  
  25.     }  
  26.   
  27.     @Override  
  28.     public List<User> queryUsers() {  
  29.         System.out.println("query all users begin");  
  30.         List<User> userList = new ArrayList<User>();  
  31.         for(User user:userMap.values()){  
  32.             userList.add(user);  
  33.         }  
  34.         System.out.println("query all users end,count:"+userList.size());  
  35.         return userList;  
  36.     }  
  37.   
  38.     @Override  
  39.     public User queryUser(int userId) {  
  40.         System.out.println("query a user begin, userId:"+userId);  
  41.         if(userMap.containsKey(userId)){  
  42.             User user = userMap.get(userId);  
  43.             System.out.println("query a user begin, userId:"+userId+", name:"+user.getName());  
  44.             return user;  
  45.         }  
  46.         System.out.println("query a user end,no the user("+userId+")");  
  47.         return null;  
  48.     }  
  49.   
  50.     @Override  
  51.     public boolean deleteUser(int userId) {  
  52.         System.out.println("delete a user begin, userId:"+userId);  
  53.         if(userMap.containsKey(userId)){  
  54.             User user = userMap.remove(userId);  
  55.             System.out.println("delete a user end, userId:"+userId+", name:"+user.getName());  
  56.         }  
  57.         return true;  
  58.     }  
  59. }  

3、实现一个WebService服务发布的工具类,用来发布WebService服务

[java] view plaincopyprint?
  1. package mysimplewebservicecxf01.publish;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import mysimplewebservicecxf01.service.interceptor.SampleInterceptorService;  
  7.   
  8. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  9.   
  10. public class ServiceManager {  
  11.     private static final ServiceManager instance = new ServiceManager();  
  12.     List<String> serviceList = new ArrayList<String>();  
  13.       
  14.     public static ServiceManager getInstance(){  
  15.         return instance;  
  16.     }  
  17.       
  18.     private ServiceManager(){  
  19.           
  20.     }  
  21.       
  22.     public void publish(Class<?> clazz, Object implObj){  
  23.         //创建WebService服务工厂  
  24.         JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
  25.         //注册WebService接口  
  26.         factory.setServiceClass(clazz);  
  27.         //发布接口  
  28.         factory.setAddress("http://localhost:9000/" +clazz.getSimpleName());  
  29.         factory.setServiceBean(implObj);  
  30. //        factory.getInInterceptors().add(new LoggingInInterceptor());   
  31. //        factory.getOutInterceptors().add(new LoggingOutInterceptor());   
  32.        factory.getInInterceptors().add(new SampleInterceptorService());  
  33.         //创建WebService  
  34.         factory.create();  
  35.     }  
  36. }  

4、最后在Activator的start函数中,使用ServiceManager 将服务发布出来
[java] view plaincopyprint?
  1. package mysimplewebservicecxf01;  
  2.   
  3. import mysimplewebservicecxf01.publish.ServiceManager;  
  4. import mysimplewebservicecxf01.user.api.IUserManager;  
  5. import mysimplewebservicecxf01.user.impl.UserManager;  
  6.   
  7. import org.osgi.framework.BundleActivator;  
  8. import org.osgi.framework.BundleContext;  
  9.   
  10. public class Activator implements BundleActivator {  
  11.   
  12.     private static BundleContext context;  
  13.   
  14.     public void start(BundleContext bundleContext) throws Exception {  
  15.         Activator.context = bundleContext;  
  16.           
  17.         ServiceManager.getInstance().publish(IUserManager.classnew UserManager());  
  18.     
  19.     }  
  20.   
  21.     public void stop(BundleContext bundleContext) throws Exception {  
  22.         Activator.context = null;  
  23.     }  
  24.   
  25. }  

  • 客户端调用服务器

[java] view plaincopyprint?
  1. package mysimplewebservicecxf01;  
  2.   
  3. import java.util.List;  
  4.   
  5. import mysimplewebservicecxf01.client.interceptor.SampleInterceptorClient;  
  6. import mysimplewebservicecxf01.user.api.IUserManager;  
  7. import mysimplewebservicecxf01.user.api.model.User;  
  8.   
  9. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  10.   
  11. public class Client {  
  12.   
  13.     public static void main(String[] args) {       
  14.          
  15.         IUserManager userMgr = (IUserManager) getService(IUserManager.class);  
  16.         List<User> userList = userMgr.queryUsers();  
  17.         int count = 0;  
  18.         /*还不清楚为什么,当返回的List为空时,客户端收到的却是null*/  
  19.         if(null != userList){  
  20.             count = userList.size();  
  21.               
  22.         }  
  23.         System.out.println("Count:"+count);  
  24.           
  25.         int userId01 = userMgr.addUser("User01");  
  26.           
  27.         userList = userMgr.queryUsers();  
  28.         count = userList.size();  
  29.         System.out.println("Count:"+count);  
  30.           
  31.         int userId02 = userMgr.addUser("User02");  
  32.           
  33.         userList = userMgr.queryUsers();  
  34.         count = userList.size();  
  35.         System.out.println("Count:"+count);  
  36.           
  37.         userMgr.deleteUser(userId02);  
  38.           
  39.         userList = userMgr.queryUsers();  
  40.         count = userList.size();  
  41.         System.out.println("Count:"+count);  
  42.     }  
  43.       
  44.     public static Object getService(Class<?> clazz){  
  45.         //创建WebService客户端代理工厂  
  46.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  47.         //注册WebService接口  
  48.         factory.setServiceClass(clazz);  
  49.         //设置WebService地址  
  50.         factory.setAddress("http://localhost:9000/"+clazz.getSimpleName());    
  51.         factory.getOutInterceptors().add(new SampleInterceptorClient());  
  52.         return factory.create();  
  53.     }  
  54.   
  55. }  
0 0
原创粉丝点击