1. import org.apache.cxf.endpoint.Client;  
  2. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  3. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;  
  4. import org.springframework.core.io.ClassPathResource;  
  5. import org.springframework.core.io.Resource;  
  6.  
  7. /**  
  8.  * <p>  
  9.  * Title: WebServiceClientHelper.java  
  10.  * </p>  
  11.  * <p>  
  12.  * Description:  
  13.  * </p>  
  14.  * <p>  
  15.  * Copyright: Copyright (c) 2009-2011  
  16.  * </p>  
  17.  * <p>  
  18.  * Company:   
  19.  * </p>  
  20.  *   
  21.  * @author erdp  
  22.  * @date 2011-8-7  
  23.  * @version 1.0  
  24.  */ 
  25. public class WebServiceClientHelper {   
  26.  
  27.     /**  
  28.      * webservice 公共接口类  
  29.      *   
  30.      * @param url webservice远程地址  
  31.      *              
  32.      * @param cls 接口参数  
  33.      *              
  34.      * @return  
  35.      */ 
  36.     public static <T> T getService(String url, Class<T> cls) {  
  37.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  38.         factory.setServiceClass(cls);  
  39.         factory.setAddress(url);  
  40.         @SuppressWarnings("unchecked")  
  41.         T service = (T) factory.create();  
  42.         return service;  
  43.     }  
  44.       
  45.     public static String callService(String wsMethod , Object[] objArr) throws Exception {  
  46.         Resource resource = new ClassPathResource(Constants.NC_WEBSERVICE_URL);  
  47.         org.codehaus.xfire.client.Client client = new org.codehaus.xfire.client.Client(resource.getInputStream(), null);   
  48.         Object[] results = client.invoke(wsMethod, objArr);  
  49.         System.out.println((String)results[0]);  
  50.         return (String)results[0];  
  51.     }  
  52.  
  53.     /**  
  54.      *   
  55.      * @param wsUrl  
  56.      * @param method  
  57.      * @param arg  
  58.      * @return  
  59.      */ 
  60.     public static String callService(String wsUrl, String method, Object arg) {  
  61.         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
  62.         Client client = dcf.createClient(wsUrl);  
  63.         Object[] res = null;  
  64.         try {  
  65.             res = client.invoke(method, arg);  
  66.             client.destroy();  
  67.             System.out.println((String) res[0]);  
  68.         } catch (Exception e) {  
  69.             e.printStackTrace();  
  70.         }  
  71.         return (String) res[0];  
  72.     }