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标注

  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. }  
package com.crazycoder2010.webservice.cxf.server;

import javax.jws.WebParam;
import javax.jws.WebService;


/**
 * 服务器端对外提供的服务
 * @author Kevin_Wang03
 *
 */
@WebService
public interface HelloWorldService {
	/**
	 * 简单的字符串参数
	 * @param userName
	 * @return
	 */
	public String sayHello(@WebParam(name="userName") String userName);
	
	/**
	 * 参数为对象的情况
	 * @param user
	 * @return
	 */
	public String sayHelloToUser(User user);
}
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. }  
package com.crazycoder2010.webservice.cxf.server;

import javax.jws.WebService;

/**
 * 默认的webservice实现
 * 
 * @author Kevin_Wang03
 * 
 */
@WebService(endpointInterface = "com.crazycoder2010.webservice.cxf.server.HelloWorldService", serviceName = "helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {

	@Override
	public String sayHello(String userName) {
		System.out.println("HelloWorldServiceImpl.sayHello("+userName+")");
		return "Hello,"+userName;
	}

	@Override
	public String sayHelloToUser(User user) {
		System.out.println("HelloWorldServiceImpl.sayHelloToUser("+user+")");
		return "Hello,("+user.getId()+","+user.getName()+")";
	}
}
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. }  
package com.crazycoder2010.webservice.cxf.server;

import javax.xml.ws.Endpoint;

public class Server {
	public static void main(String[] args) {
		System.out.println("Starting Server");
		HelloWorldServiceImpl helloWorldServiceImpl = new HelloWorldServiceImpl();
		String address = "http://localhost:9000/helloWorldService";
		Endpoint.publish(address, helloWorldServiceImpl);
		System.out.println("Start success");
	}
}
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.crazycoder2010.com/}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  
Starting Server
Aug 9, 2011 5:27:29 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://server.cxf.webservice.crazycoder2010.com/}helloWorldService from class com.crazycoder2010.webservice.cxf.server.HelloWorldService
Aug 9, 2011 5:27:29 PM org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the server's publish address to be http://localhost:9000/helloWorldService
2011-08-09 17:27:29.895:INFO::jetty-7.4.2.v20110526
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"/>

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

  1. <?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">  
  2.   <wsdl:types>  
  3. <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">  
  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.xmlsoap.org/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>  
<?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">
  <wsdl:types>
<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:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="sayHelloToUser" type="sayHelloToUser"></xs:element>

  <xs:complexType name="sayHelloToUser">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="user"></xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="sayHelloToUserResponse" type="sayHelloToUserResponse"></xs:element>
  <xs:complexType name="sayHelloToUserResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"></xs:element>

    </xs:sequence>
  </xs:complexType>
  <xs:element name="sayHello" type="sayHello"></xs:element>
  <xs:complexType name="sayHello">
    <xs:sequence>
      <xs:element minOccurs="0" name="userName" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="sayHelloResponse" type="sayHelloResponse"></xs:element>

  <xs:complexType name="sayHelloResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="sayHelloToUser">
    <wsdl:part element="tns:sayHelloToUser" name="parameters">

    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sayHelloToUserResponse">
    <wsdl:part element="tns:sayHelloToUserResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="tns:sayHelloResponse" name="parameters">
    </wsdl:part>

  </wsdl:message>
  <wsdl:message name="sayHello">
    <wsdl:part element="tns:sayHello" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="HelloWorldService">
    <wsdl:operation name="sayHelloToUser">
      <wsdl:input message="tns:sayHelloToUser" name="sayHelloToUser">
    </wsdl:input>

      <wsdl:output message="tns:sayHelloToUserResponse" name="sayHelloToUserResponse">
    </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="sayHello">
      <wsdl:input message="tns:sayHello" name="sayHello">
    </wsdl:input>
      <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
    </wsdl:output>
    </wsdl:operation>

  </wsdl:portType>
  <wsdl:binding name="helloWorldServiceSoapBinding" type="tns:HelloWorldService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
    <wsdl:operation name="sayHelloToUser">
      <soap:operation soapAction="" style="document"></soap:operation>
      <wsdl:input name="sayHelloToUser">
        <soap:body use="literal"></soap:body>
      </wsdl:input>
      <wsdl:output name="sayHelloToUserResponse">

        <soap:body use="literal"></soap:body>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document"></soap:operation>
      <wsdl:input name="sayHello">
        <soap:body use="literal"></soap:body>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">

        <soap:body use="literal"></soap:body>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="helloWorldService">
    <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="HelloWorldServiceImplPort">
      <soap:address location="http://localhost:9000/helloWorldService"></soap:address>
    </wsdl:port>
  </wsdl:service>

</wsdl:definitions>
小结:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值