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

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(服务接口),其他和普通类没有任何区别

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!

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类
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"/>

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

<?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、付费专栏及课程。

余额充值