CXF創建webservice客戶端和服務端


一、CXF的介紹

Apache CXF是一個開源的WebService框架,CXF大大簡化了Webservice的創建,同時它繼承了XFire的傳統,一樣可以和spring天然的進行無縫的集成。CXF框架是一種基於servlet技術的SOA應用開發框架,要正常運用基於CXF應用框架開發的企業應用,除了CXF應用本身之外,還需要JDK和servlet容器的支持。

二、CXF的准備條件

所需要的jar包:

  xmlbeans-2.4.0.jar

  wss4j-1.5.9.jar

  jetty-server-7.1.6.v20100715.jar

  jetty-util-7.1.6.v20100715.jar

  geronimo-ws-metadata_2.0_spec-1.1.3.jar

  geronimo-activation_1.1_spec-1.1.jar

  geronimo-servlet_3.0_spec-1.0.jar

  velocity-1.6.4.jar

  jaxb-xjc-2.2.1.1.jar

  xml-resolver-1.2.jar

  wsdl4j-1.6.2.jar

  cxf-2.3.0.jar

  XmlSchema-1.4.7.jar

  jaxb-api-2.2.1.jar

  jaxb-impl-2.2.1.1.jar

  neethi-2.0.4.jar

  geronimo-annotation_1.0_spec-1.1.1.jar

  geronimo-stax-api_1.0_spec-1.0.1.jar

下載地址:http://download.csdn.net/detail/ch656409110/5748183   (取自己需要的jar包)

 

三、創建webservice服務端

1、先將jar包放入lib目錄

2、在web.xml中配置CXF框架的核心servlet

 

[html]  view plain  copy
 
 print?
  1. <!-- CXF -->  
  2. <servlet>    
  3.     <servlet-name>CXFServlet</servlet-name>    
  4.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
  5.     <load-on-startup>1</load-on-startup>    
  6. </servlet>    
  7. <servlet-mapping>    
  8.     <servlet-name>CXFServlet</servlet-name>    
  9.     <url-pattern>/services/*</url-pattern>    
  10. </servlet-mapping>  

 

3、在applicationContext.xml中導入xml,並且發布webservice服務。

 

 

[html]  view plain  copy
 
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:aop="http://www.springframework.org/schema/aop"  
  8.     xmlns:jaxws="http://cxf.apache.org/jaxws"    
  9.     xmlns:jaxrs="http://cxf.apache.org/jaxrs"    
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  11.                         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  12.                         http://www.springframework.org/schema/tx  
  13.                         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  14.                         http://www.springframework.org/schema/aop  
  15.                         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  16.                         http://cxf.apache.org/jaxws    
  17.                         http://cxf.apache.org/schemas/jaxws.xsd    
  18.                         http://cxf.apache.org/jaxrs    
  19.                         http://cxf.apache.org/schemas/jaxrs.xsd  
  20.                         "  
  21.                         default-autowire="byName"  
  22.                         >  
  23.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  24.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  25.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  26.       
  27.     <!-- <jaxws:endpoint id="facelookWebService" address="/facelookWebService" implementor="com.facelook.webservice.server.FacelookWebServiceImpl"></jaxws:endpoint> -->  
  28.     <!-- 不知道為什么,這里的webservice配置,只能用bean來實現,否則 注入的service為空。但是之前有個項目卻可以,百思不得其解。。 -->  
  29.     <bean id="facelookWebService" class="com.facelook.webservice.server.FacelookWebServiceImpl"/>   
  30.     <jaxws:endpoint id="facelookWebService1" address="/facelookWebService" implementorClass="com.facelook.webservice.server.FacelookWebServiceImpl">  
  31.         <jaxws:implementor ref="facelookWebService"/>    
  32.     </jaxws:endpoint>  
  33. </beans>  

 

4、定義webservice接口FacelookWebService 和 實現類FacelookWebServiceImpl。

 

 

[java]  view plain  copy
 
 print?
  1. @WebService  
  2. public interface FacelookWebService {  
  3.       
  4.     /** 
  5.      * 根據傳遞的條件獲取相冊信息 
  6.      * xml的格式規范 
  7.      * <?xml version=\"1.0\" encoding=\"UTF-8\"?> 
  8.      * <facelook> 
  9.      *  <condition> 
  10.      *      <name></name> 
  11.      *      <description></description> 
  12.      *      <pageno></pageno> 
  13.      *      <pagesize></pagesize> 
  14.      *  </condition> 
  15.      * </facelook> 
  16.      * 這里的WebParam必須指定,否則調用的時候返回null 
  17.      * @return 
  18.      */  
  19.     public String getAlbumList(@WebParam(name="xmlStr") String xmlStr);  
  20. }  
  21.   
  22.   
  23. @WebService  
  24. //這后面的可以不寫注釋后面的配置,在applicationContext配置也一樣(serviceName="facelookWebService",endpointInterface="com.facelook.webservice.server.FacelookWebService")  
  25. public class FacelookWebServiceImpl implements FacelookWebService{  
  26.   
  27.     @Autowired  
  28.     private AlbumService albumService;  
  29.       
  30.     @Override  
  31.     public String getAlbumList(String xmlStr) {  
  32.         try {  
  33.             List<Album> albumList = getAlbumPage(xmlStr);  
  34.             JSONArray jsonArray = JSONArray.fromObject(albumList);  
  35.             return jsonArray.toString();  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.         return null;  
  40.     }  
  41. }   

這樣子,基本上就可以了。

 

5、保存代碼,發布項目,啟動tomact。

在地址欄輸入:http://localhost:8080/house/services/houseWebService?wsdl  即可看到發布的服務端的明細。

顯示如下:

這就表示CXF發布的webservice服務端成功了。

6、通過客戶端調用服務端webservice。

axis的客戶端訪問:

 

[java]  view plain  copy
 
 print?
  1. public static void main(String[] args) throws ServiceException, RemoteException, MalformedURLException {  
  2.     String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"  
  3.              + "     <facelook>"  
  4.              + "        <condition>"  
  5.              + "            <name>家</name>"  
  6.              + "            <description></description>"  
  7.              + "            <pageno></pageno>"  
  8.              + "            <pagesize></pagesize>"  
  9.              + "        </condition>"  
  10.              + "     </facelook>";  
  11.       
  12.       Service service = new Service();  
  13.       Call call = (Call) service.createCall();  
  14.       call.setTargetEndpointAddress("http://localhost:8080/facelook/services/facelookWebService?wsdl");  
  15.       QName qName = new QName("http://server.webservice.facelook.com/", "getAlbumList");  
  16.       call.setOperationName(qName);  
  17.       call.setUseSOAPAction(true);  
  18.       //這下面兩行一定要加上,否則接收在服務器端收不到。  
  19.       call.addParameter("xmlStr", XMLType.XSD_STRING, ParameterMode.IN);  
  20.       call.setReturnType(XMLType.XSD_STRING);  
  21.       String result = (String) call.invoke(new Object[] { xmlStr });  
  22.       System.out.println(result);  
  23.       
  24.     //將返回的字符串轉換成list集合  
  25.     //JSONArray array = JSONArray.fromObject(result);  
  26.     //List<Album> list = JSONArray.toList(array,Album.class);  
  27.       
  28. }  

 

 

CXF客戶端訪問:

 

[java]  view plain  copy
 
 print?
  1. public static void main(String[] args) throws Exception {  
  2.         //這個是用cxf 客戶端訪問cxf部署的webservice服務  
  3.         //千萬記住,訪問cxf的webservice必須加上namespace ,否則通不過  
  4.         //現在又另外一個問題,傳遞過去的參數服務端接收不到  
  5.         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
  6.         org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/facelook/services/facelookWebService?wsdl");  
  7.         //url為調用webService的wsdl地址  
  8.         QName name=new QName("http://server.webservice.facelook.com/","getAlbumList");  
  9.         //namespace是命名空間,methodName是方法名  
  10.         String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"  
  11.                  + "     <facelook>"  
  12.                  + "        <condition>"  
  13.                  + "            <name>家</name>"  
  14.                  + "            <description></description>"  
  15.                  + "            <pageno></pageno>"  
  16.                  + "            <pagesize></pagesize>"  
  17.                  + "        </condition>"  
  18.                  + "     </facelook>";  
  19.         //paramvalue為參數值  
  20.         Object[] objects=client.invoke(name,xmlStr);   
  21.         //調用web Service//輸出調用結果  
  22.         System.out.println(objects[0].toString());  
  23. }  


在這里面傳遞的xml規范由 服務端自己規范好了,然后去解析、獲取參數,執行相應的操作,返回想要的結果給調用的客戶端。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM