温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
        最近由于项目需要,一直在学习OSGI,在学习OSGI的这段时间内,不断的接触到apache的一些优秀的开源项目,比如说FelixCXF等。Felix是Apache对OSGI R4规范的一个轻量级实现。你使用eclipse创建的plugin(插件)工程都是可以正常运行在Felix中的。前提是你创建bundle的时候选择标准选项这一栏。好了本篇文章主要是用来介绍CXF的,关于Felix就不再深入讨论了,有兴趣的可以自行去研究下。
        关于CXF,不做过多的解释。官方的解释已经够清楚了。相信大家之前在Java环境下创建webservice程序大多数选择的是xfire这个框架吧。后来好多专家不再推荐这个东东。都建议使用CXF。在未接触到CXF之前,本人一向喜欢用xfire这个框架来创建自己的webservice。还了,废话不多说,先来看个HelloWorld的程序,教大家快速上手。
        首先去Apache网站下载CXF所需要的jar,我本人下载是apache-cxf-2.2.10.zip这个包。这里为了方便期间创建一个java工程。啊?java工程,这有点不可思议了,不是要创建webservice吗?怎么是java工程?呵呵,这里就是CXF的神奇之处!
        添加必须的jar到你的classpath路径下。
        cxf-2.2.10.jar 核心jar
        jetty-6.1.21.jar 用来启动jetty服务器
        jetty-util-6.1.21.jar jetty辅助工具
        wsdl4j-1.6.2.jar wsdl支持工具
        XmlSchema-1.4.5.jar 
        这就是CXF的最小配置,以上jar包缺一不可
        创建一个接口
package com.cxf.service;

public interface HelloWorldCxfService {
    
    String sayHello(String username);
}
        创建该接口的实现类
package com.cxf.service;

public class HelloWorldCxfServiceImpl implements HelloWorldCxfService {

    
public String sayHello(String username) {
        
return "Hello,"+username;
    }
}
        发布webservice
package com.cxf.server;

import org.apache.cxf.frontend.ServerFactoryBean;

import com.cxf.service.HelloWorldCxfService;
import com.cxf.service.HelloWorldCxfServiceImpl;

public class Server {
    
    
public static void main(String[] args){
        HelloWorldCxfServiceImpl worldCxfServiceImpl
=new HelloWorldCxfServiceImpl();
        ServerFactoryBean factoryBean
=new ServerFactoryBean();
        factoryBean.setAddress(
"http://localhost:8080/hello");
        factoryBean.setServiceClass(HelloWorldCxfService.
class);
        factoryBean.setServiceBean(worldCxfServiceImpl);
        factoryBean.create();
    }
}
        运行Server,注意不要关闭,在控制台会打印如下信息:
2010-9-10 9:44:16 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
2010-9-10 9:44:16 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8080/hello
2010-09-10 09:44:16.296::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
2010-09-10 09:44:16.296::INFO:  jetty-6.1.21
2010-09-10 09:44:16.390::INFO:  Started SelectChannelConnector@localhost:8080
        客户端调用
package com.cxf.server;

import org.apache.cxf.frontend.ClientProxyFactoryBean;

import com.cxf.service.HelloWorldCxfService;

public class Client {
    
    
public static void main(String[] args) {
        ClientProxyFactoryBean factoryBean
=new ClientProxyFactoryBean();
        factoryBean.setAddress(
"http://localhost:8080/hello");
        factoryBean.setServiceClass(HelloWorldCxfService.
class);
        HelloWorldCxfService worldCxfService
=(HelloWorldCxfService) factoryBean.create();
        System.out.println(worldCxfService.sayHello(
"张三"));
    }
}
        运行Client代码,控制台打印如下信息:
2010-9-10 9:46:58 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
Hello,张三
        到此,我们的webservice,已经成功调用了。大家是不是迫不及待的想看下wsdl文件是啥样的呢?
在浏览器中输入http://localhost:8080/hello?wsdl,即可看到wsdl文件了。其中http://localhost:8080/hello部分为代码里指定的Address。
        wsdl文件信息:
  <?xml version="1.0" ?> 
<wsdl:definitions name="HelloWorldCxfService" targetNamespace="http://service.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://service.cxf.com/" xmlns:tns="http://service.cxf.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  
<xsd:element name="sayHello" type="tns:sayHello" /> 
<xsd:complexType name="sayHello">
<xsd:sequence>
  
<xsd:element minOccurs="0" name="arg0" type="xsd:string" /> 
  
</xsd:sequence>
  
</xsd:complexType>
  
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse" /> 
<xsd:complexType name="sayHelloResponse">
<xsd:sequence>
  
<xsd:element minOccurs="0" name="return" type="xsd:string" /> 
  
</xsd:sequence>
  
</xsd:complexType>
  
</xsd:schema>
  
</wsdl:types>
<wsdl:message name="sayHelloResponse">
  
<wsdl:part element="tns:sayHelloResponse" name="parameters" /> 
  
</wsdl:message>
<wsdl:message name="sayHello">
  
<wsdl:part element="tns:sayHello" name="parameters" /> 
  
</wsdl:message>
<wsdl:portType name="HelloWorldCxfServicePortType">
<wsdl:operation name="sayHello">
  
<wsdl:input message="tns:sayHello" name="sayHello" /> 
  
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> 
  
</wsdl:operation>
  
</wsdl:portType>
<wsdl:binding name="HelloWorldCxfServiceSoapBinding" type="tns:HelloWorldCxfServicePortType">
  
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
<wsdl:operation name="sayHello">
  
<soap:operation soapAction="" style="document" /> 
<wsdl:input name="sayHello">
  
<soap:body use="literal" /> 
  
</wsdl:input>
<wsdl:output name="sayHelloResponse">
  
<soap:body use="literal" /> 
  
</wsdl:output>
  
</wsdl:operation>
  
</wsdl:binding>
<wsdl:service name="HelloWorldCxfService">
<wsdl:port binding="tns:HelloWorldCxfServiceSoapBinding" name="HelloWorldCxfServicePort">
  
<soap:address location="http://localhost:8080/hello" /> 
  
</wsdl:port>
  
</wsdl:service>
  
</wsdl:definitions>
                
posted on 2010-09-10 09:51 雪山飞鹄 阅读(5942) 评论(7)  编辑  收藏 所属分类: webservice

Feedback

# re: Apache之CXF HelloWorld入门[未登录] 2010-09-10 10:00 yang
这不类似 xml-rpc 了么???  回复  更多评论
  

# re: Apache之CXF HelloWorld入门 2010-09-10 12:57 cxh8318
能否告知如何实现jar包的最小配置?是一个一个试吗?  回复  更多评论
  

# re: Apache之CXF HelloWorld入门 2010-09-10 13:47 @joe
这是基于c/s的  回复  更多评论
  

# re: Apache之CXF HelloWorld入门 2010-09-10 14:10 java小爬虫
没任何技术含量,是cxf在线文档的摘抄罢了...  回复  更多评论
  

# re: Apache之CXF HelloWorld入门 2010-09-10 14:16 雪山飞鹄
@java小爬虫
本人才学疏浅,刚开始接触CXF。我已明确说了是CXF入门。再者,本人写这篇博文的时候并未参考过CXF的在线文档。  回复  更多评论
  

# re: Apache之CXF HelloWorld入门 2010-09-10 14:21 雪山飞鹄
@cxh8318
关于jar包的最小配置,我一般是尝试的。我一直不建议,将框架下的所有jar添加到工程中,这样一来体积明显较大,而且很容易出现jar包冲突。我一般是这么做的,先只添加核心jar,然后运行它。在运行过程中根据控制台的异常信息,去添加对应所需的jar。这样即快又方便,还能加深印象。即使时间再长,也不会忘记它依赖那些jar。  回复  更多评论
  

# re: Apache之CXF HelloWorld入门 2012-12-05 16:22 jungle
有用有用,那个说“没任何技术含量,是cxf在线文档的摘抄”自己不分享还在那里叫叫叫,自私的要命。

我可以运行成功。如果楼主有WEB方面的helloworld发我一份吧。要有JAR包哦~61917380@qq.com  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: