分享

CXF学习笔记(1)

 馆天下ccf 2014-12-04
  1.apache网站下载CXF
  http://cxf./download.html
  最新版本2.4.1
2.创建一个java工程,将以下jar包复制到工程的classpath下

  所有的jar包都可以在${CXF_HOME}\lib目录中找到

3.定义服务接口HelloWorldService

因为这个接口将会被我们暴露为webservice,所以给该接口加一个@WebService标注

  1. package com.crazycoder2010.webservice.cxf.server;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6.   
  7. /** 
  8.  * 服务器端对外提供的服务 
  9.  * @author Kevin_Wang03 
  10.  * 
  11.  */  
  12. @WebService  
  13. public interface HelloWorldService {  
  14.     /** 
  15.      * 简单的字符串参数 
  16.      * @param userName 
  17.      * @return 
  18.      */  
  19.     public String sayHello(@WebParam(name="userName") String userName);  
  20.       
  21.     /** 
  22.      * 参数为对象的情况 
  23.      * @param user 
  24.      * @return 
  25.      */  
  26.     public String sayHelloToUser(User user);  
  27. }  
4.提供具体的webservice提供者HelloWorldServiceImpl

这个实现类实现了我们上面的服务接口,除了要添加@WebService标注外,还要定义该服务的名称serviceName="helloWorldService" 和endpoint(服务接口),其他和普通类没有任何区别

  1. package com.crazycoder2010.webservice.cxf.server;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. /** 
  6.  * 默认的webservice实现 
  7.  *  
  8.  * @author Kevin_Wang03 
  9.  *  
  10.  */  
  11. @WebService(endpointInterface = "com.crazycoder2010.webservice.cxf.server.HelloWorldService", serviceName = "helloWorldService")  
  12. public class HelloWorldServiceImpl implements HelloWorldService {  
  13.   
  14.     @Override  
  15.     public String sayHello(String userName) {  
  16.         System.out.println("HelloWorldServiceImpl.sayHello("+userName+")");  
  17.         return "Hello,"+userName;  
  18.     }  
  19.   
  20.     @Override  
  21.     public String sayHelloToUser(User user) {  
  22.         System.out.println("HelloWorldServiceImpl.sayHelloToUser("+user+")");  
  23.         return "Hello,("+user.getId()+","+user.getName()+")";  
  24.     }  
  25. }  
4.通过JAX-WS将类发布为服务

这个是个普通类,要发布一个webservice,首先要定义好这个webservice的访问地址和端口,然后需要知道把哪个类发布成服务,就这么简单?yes!

  1. package com.crazycoder2010.webservice.cxf.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. public class Server {  
  6.     public static void main(String[] args) {  
  7.         System.out.println("Starting Server");  
  8.         HelloWorldServiceImpl helloWorldServiceImpl = new HelloWorldServiceImpl();  
  9.         String address = "http://localhost:9000/helloWorldService";  
  10.         Endpoint.publish(address, helloWorldServiceImpl);  
  11.         System.out.println("Start success");  
  12.     }  
  13. }  
5.运行Server类
  1. Starting Server  
  2. Aug 9, 2011 5:27:29 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass  
  3. INFO: Creating Service {http://server.cxf.webservice./}helloWorldService from class com.crazycoder2010.webservice.cxf.server.HelloWorldService  
  4. Aug 9, 2011 5:27:29 PM org.apache.cxf.endpoint.ServerImpl initDestination  
  5. INFO: Setting the server's publish address to be http://localhost:9000/helloWorldService  
  6. 2011-08-09 17:27:29.895:INFO::jetty-7.4.2.v20110526  
  7. Start success  
6.通过URL访问WSDL看看是否显示正常

   http://localhost:9000/helloWorldService?wsdl

分析输出的wsdl文件

 6.1<wsdl:definitions name="helloWorldService" >

       这个name就是我们在HelloWorldServiceImpl类的标注serviceName="helloWorldService"生成的

 6.2<wsdl:definitions targetNamespace="http://server.cxf.webservice./">

      注意到我们的代码位于com.crazycoder2010.webservice.cxf.server中,记得sun推荐的包的命名标准吗?就是公司域名翻转加上项目名,那把包名翻过来是什么?-你懂得,CXF就用了这种机制

  6.3<xs:complexType name="sayHello">

       这个即是我们定义在服务接口中的方法,在wsdl文件中将接口中的方法定义成服务方法,里面的子元素定义了该方法的参数

  6.4<xs:complexType name="sayHelloResponse">

      这个是对接口服务方法返回值的描述,默认遵循以下约定对方法的描述为sayHello,则返回值的描述为sayHelloResponse

  6.5<wsdl:message name="sayHelloToUser">

      这个描述把服务和方法绑定起来,与Response是成对出现的

  6.6<soap:address location="http://localhost:9000/helloWorldService"/>

     这里就是我们上面在代码里完成的服务的访问地址

  1. <?xml version="1.0" ?><wsdl:definitions name="helloWorldService" targetNamespace="http://server.cxf.webservice./" xmlns:ns1="http://schemas./soap/http" xmlns:soap="http://schemas./wsdl/soap/" xmlns:tns="http://server.cxf.webservice./" xmlns:wsdl="http://schemas./wsdl/" xmlns:xsd="http://www./2001/XMLSchema">  
  2.   <wsdl:types>  
  3. <xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://server.cxf.webservice./" xmlns="http://server.cxf.webservice./" xmlns:xs="http://www./2001/XMLSchema">  
  4.   <xs:complexType name="user">  
  5.     <xs:sequence>  
  6.       <xs:element name="id" type="xs:int"></xs:element>  
  7.       <xs:element minOccurs="0" name="name" type="xs:string"></xs:element>  
  8.     </xs:sequence>  
  9.   </xs:complexType>  
  10.   <xs:element name="sayHelloToUser" type="sayHelloToUser"></xs:element>  
  11.   
  12.   <xs:complexType name="sayHelloToUser">  
  13.     <xs:sequence>  
  14.       <xs:element minOccurs="0" name="arg0" type="user"></xs:element>  
  15.     </xs:sequence>  
  16.   </xs:complexType>  
  17.   <xs:element name="sayHelloToUserResponse" type="sayHelloToUserResponse"></xs:element>  
  18.   <xs:complexType name="sayHelloToUserResponse">  
  19.     <xs:sequence>  
  20.       <xs:element minOccurs="0" name="return" type="xs:string"></xs:element>  
  21.   
  22.     </xs:sequence>  
  23.   </xs:complexType>  
  24.   <xs:element name="sayHello" type="sayHello"></xs:element>  
  25.   <xs:complexType name="sayHello">  
  26.     <xs:sequence>  
  27.       <xs:element minOccurs="0" name="userName" type="xs:string"></xs:element>  
  28.     </xs:sequence>  
  29.   </xs:complexType>  
  30.   <xs:element name="sayHelloResponse" type="sayHelloResponse"></xs:element>  
  31.   
  32.   <xs:complexType name="sayHelloResponse">  
  33.     <xs:sequence>  
  34.       <xs:element minOccurs="0" name="return" type="xs:string"></xs:element>  
  35.     </xs:sequence>  
  36.   </xs:complexType>  
  37. </xs:schema>  
  38.   </wsdl:types>  
  39.   <wsdl:message name="sayHelloToUser">  
  40.     <wsdl:part element="tns:sayHelloToUser" name="parameters">  
  41.   
  42.     </wsdl:part>  
  43.   </wsdl:message>  
  44.   <wsdl:message name="sayHelloToUserResponse">  
  45.     <wsdl:part element="tns:sayHelloToUserResponse" name="parameters">  
  46.     </wsdl:part>  
  47.   </wsdl:message>  
  48.   <wsdl:message name="sayHelloResponse">  
  49.     <wsdl:part element="tns:sayHelloResponse" name="parameters">  
  50.     </wsdl:part>  
  51.   
  52.   </wsdl:message>  
  53.   <wsdl:message name="sayHello">  
  54.     <wsdl:part element="tns:sayHello" name="parameters">  
  55.     </wsdl:part>  
  56.   </wsdl:message>  
  57.   <wsdl:portType name="HelloWorldService">  
  58.     <wsdl:operation name="sayHelloToUser">  
  59.       <wsdl:input message="tns:sayHelloToUser" name="sayHelloToUser">  
  60.     </wsdl:input>  
  61.   
  62.       <wsdl:output message="tns:sayHelloToUserResponse" name="sayHelloToUserResponse">  
  63.     </wsdl:output>  
  64.     </wsdl:operation>  
  65.     <wsdl:operation name="sayHello">  
  66.       <wsdl:input message="tns:sayHello" name="sayHello">  
  67.     </wsdl:input>  
  68.       <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">  
  69.     </wsdl:output>  
  70.     </wsdl:operation>  
  71.   
  72.   </wsdl:portType>  
  73.   <wsdl:binding name="helloWorldServiceSoapBinding" type="tns:HelloWorldService">  
  74.     <soap:binding style="document" transport="http://schemas./soap/http"></soap:binding>  
  75.     <wsdl:operation name="sayHelloToUser">  
  76.       <soap:operation soapAction="" style="document"></soap:operation>  
  77.       <wsdl:input name="sayHelloToUser">  
  78.         <soap:body use="literal"></soap:body>  
  79.       </wsdl:input>  
  80.       <wsdl:output name="sayHelloToUserResponse">  
  81.   
  82.         <soap:body use="literal"></soap:body>  
  83.       </wsdl:output>  
  84.     </wsdl:operation>  
  85.     <wsdl:operation name="sayHello">  
  86.       <soap:operation soapAction="" style="document"></soap:operation>  
  87.       <wsdl:input name="sayHello">  
  88.         <soap:body use="literal"></soap:body>  
  89.       </wsdl:input>  
  90.       <wsdl:output name="sayHelloResponse">  
  91.   
  92.         <soap:body use="literal"></soap:body>  
  93.       </wsdl:output>  
  94.     </wsdl:operation>  
  95.   </wsdl:binding>  
  96.   <wsdl:service name="helloWorldService">  
  97.     <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="HelloWorldServiceImplPort">  
  98.       <soap:address location="http://localhost:9000/helloWorldService"></soap:address>  
  99.     </wsdl:port>  
  100.   </wsdl:service>  
  101.   
  102. </wsdl:definitions>  
小结:

HelloWorld 是跑起来了,感觉比先前用的JAX-RPC要简单的多了,几分钟就可以搞定一个webservice--也许这就是框架的魅力,但是也有一些疑问,如要运行这么小一个helloWorld,要依赖20几个jar包,其中还有Jetty的jar包若干!竟然还有org.eclipse.jetty.*这种包,不知道设计这个框架的同学是怎么考虑的,为什么要把webservice和jetty或eclipse扯上关系,可能看的不够深,明天继续研究。



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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多