微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CXF学习笔记(1)-HelloWorld!-发布webservice

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

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

3.定义服务接口HelloWorldService

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

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

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

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

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

?
    import javax.xml.ws.Endpoint;  
  1. class Server {  
  2. static void main(String[] args) {  
  3.         System.out.println("Starting Server");  
  4.         HelloWorldServiceImpl helloWorldServiceImpl = new HelloWorldServiceImpl();  
  5.         String address = "http://localhost:9000/helloWorldService";  
  6.         Endpoint.publish(address, helloWorldServiceImpl);  
  7.         System.out.println("Start success");  
  8. }  
5.运行Server类
[html]  ?
    Starting Server  
  1. Aug 9, 2011 5:27:29 PM org.apache.cxf.service.factory.ReflectionServicefactorybean buildServiceFromClass  
  2. INFO: Creating Service {http://server.cxf.webservice.crazycoder2010.com/}helloWorldService from class com.crazycoder2010.webservice.cxf.server.HelloWorldService  
  3. INFO: Setting the server's publish address to be http://localhost:9000/helloWorldService  
  4. 2011-08-09 17:27:29.895:INFO::jetty-7.4.2.v20110526  
  5. 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.crazycoder2010.com/">

      注意到我们的代码位于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"/>

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

?
    <?xml version="1.0" ?><wsdl:deFinitions name="helloWorldService" targetNamespace="http://server.cxf.webservice.crazycoder2010.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.cxf.webservice.crazycoder2010.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  1.   wsdl:types>  
  2. xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://server.cxf.webservice.crazycoder2010.com/" xmlns="http://server.cxf.webservice.crazycoder2010.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"xs:complexType name="user"     xs:sequence       xs:element name="id" type="xs:int"></xs:element       xs:element minOccurs="0" name="name" type="xs:string"        xs:complexTypexs:element name="sayHelloToUser" type="sayHelloToUser"xs:complexType name="sayHelloToUser"xs:element minOccurs="0" name="arg0" type="user"xs:element name="sayHelloToUserResponse" type="sayHelloToUserResponse"xs:complexType name="sayHelloToUserResponse"xs:element minOccurs="0" name="return" type="xs:string"xs:element name="sayHello" type="sayHello"xs:complexType name="sayHello"xs:element minOccurs="0" name="userName" type="xs:string"xs:element name="sayHelloResponse" type="sayHelloResponse"xs:complexType name="sayHelloResponse"xs:schemawsdl:message name="sayHelloToUser"wsdl:part element="tns:sayHelloToUser" name="parameters"wsdl:partwsdl:messagewsdl:message name="sayHelloToUserResponse"wsdl:part element="tns:sayHelloToUserResponse" name="parameters"wsdl:message name="sayHelloResponse"wsdl:part element="tns:sayHelloResponse" name="parameters"wsdl:message name="sayHello"wsdl:part element="tns:sayHello" name="parameters"wsdl:portType name="HelloWorldService"wsdl:operation name="sayHelloToUser"wsdl:input message="tns:sayHelloToUser" name="sayHelloToUser"wsdl:inputwsdl:output message="tns:sayHelloToUserResponse" name="sayHelloToUserResponse"wsdl:outputwsdl:operationwsdl:operation name="sayHello"wsdl:input message="tns:sayHello" name="sayHello"wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"wsdl:portTypewsdl:binding name="helloWorldServiceSoapBinding" type="tns:HelloWorldService"soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"soap:bindingsoap:operation soapAction="" style="document"soap:operationwsdl:input name="sayHelloToUser"         soap:body use="literal"soap:bodywsdl:output name="sayHelloToUserResponse"wsdl:input name="sayHello"wsdl:output name="sayHelloResponse"wsdl:bindingwsdl:service name="helloWorldService"wsdl:port binding="tns:helloWorldServiceSoapBinding" name="HelloWorldServiceImplPort"soap:address location="http://localhost:9000/helloWorldService"soap:addresswsdl:portwsdl:service wsdl:deFinitions>  
小结:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐