CXF使用备忘

在javaeye里参考大牛们的帖子去配置spring2.5+hibernate3.2+CXF2.1,最终解决了路径问题后,终于成功了!
CXF是Apache的一个重点项目,终于放出来了,因为它跟spring的结合很方便,于是就用一下,还是要自己动手一步步弄一下,才知道其配置上的一些小细节(如果用插件的话,就感觉不到它跟spring的关系和不知道它的很多细节,所以推荐不要用插件的好)。
在这里顺带提一下,spring2.5结合junit4.4可以很容易地运用annotation来进行testcase的编写,不过要注意的是eclipse3.3或者eclipose3.4,里头自带的junit4是junit4.3版本的,缺少需要的方法,所以要去下载最新的junit4.4版,然后替换掉eclipse插件里的junit.jar包。
  1. 准备依赖包,依赖包不用想那么多,我在这里是把握的项目包弄个截图,所以是很多的,重点是spring,hibernate,CXF*这些包。
    o_cxf_jars_pics.JPG
  2. 在web.xml文件中添加以下servlet配置:
    None.gif <!--  CXF 配置  -->
    None.gif    
    < servlet >
    None.gif        
    < servlet-name > CXFServlet </ servlet-name >
    None.gif        
    < servlet-class > org.apache.cxf.transport.servlet.CXFServlet </ servlet-class >
    None.gif        
    < load-on-startup > 1 </ load-on-startup >
    None.gif    
    </ servlet >
    None.gif    
    < servlet-mapping >
    None.gif        
    < servlet-name > CXFServlet </ servlet-name >
    None.gif        
    < url-pattern > /services/* </ url-pattern >
    None.gif    
    </ servlet-mapping >

  3. 添加applicationContext-cxf.xml文件到上下文,配置如下:
    None.gif <? xml version="1.0" encoding="UTF-8" ?>
    None.gif
    < beans  xmlns ="http://www.springframework.org/schema/beans"
    None.gif    xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
    None.gif    xmlns:jaxws
    ="http://cxf.apache.org/jaxws"
    None.gif    xsi:schemaLocation
    ="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
    None.gif    default-autowire
    ="byName"  default-lazy-init ="true" >
    None.gif    
    < description > 基于Apache CXF的Web Service配置文件 </ description >
    None.gif    
    None.gif    
    < import  resource ="classpath:META-INF/cxf/cxf.xml" />
    None.gif    
    < import  resource ="classpath:META-INF/cxf/cxf-servlet.xml" />
    None.gif    
    < import  resource ="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    None.gif    
    < import
    None.gif        
    resource ="classpath:META-INF/cxf/cxf-extension-javascript-client.xml" />
    None.gif    
    None.gif    
    < bean  id ="helloWorldImpl"  class ="com.lbg.ws.test.impl.HelloWorld" />
    None.gif    
    None.gif    
    < jaxws:endpoint  id ="helloWorld"  implementor ="#helloWorldImpl"
    None.gif        address
    ="/HelloWorld" />
    None.gif
    </ beans >

  4. 建立webservice的接口与实现:
    None.gif package  com.lbg.ws.test;
    None.gif
    None.gif
    import  javax.jws.WebMethod;
    None.gif
    import  javax.jws.WebService;
    None.gif
    None.gif@WebService(name
    = " HelloWorld " )
    ExpandedBlockStart.gifContractedBlock.gif
    public   interface  IHelloWorld  dot.gif {
    InBlock.gif
    InBlock.gif    @WebMethod(operationName
    ="sayHello")
    InBlock.gif    
    public String sayHello();
    ExpandedBlockEnd.gif}

    None.gif package  com.lbg.ws.test.impl;
    None.gif
    None.gif
    import  javax.jws.WebService;
    None.gif
    None.gif
    import  com.lbg.ws.test.IHelloWorld;
    None.gif
    None.gif@WebService(endpointInterface
    = " com.lbg.ws.test.IHelloWorld " )
    ExpandedBlockStart.gifContractedBlock.gif
    public   class  HelloWorld  implements  IHelloWorld  dot.gif {
    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    public String sayHello() dot.gif{
    InBlock.gif        
    return "HelloWorld!";
    ExpandedSubBlockEnd.gif    }

    ExpandedBlockEnd.gif}
  5. 把项目部署到tomcat或其它j2ee容器上启动,成功信息如下:
    None.gif 2008 - 6 - 30   21 : 16 : 57  org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
    None.gif信息: Creating Service {http://impl.test.ws.lbg.com/}HelloWorldService from class com.lbg.ws.test.IHelloWorld
    None.gif
    2008 - 6 - 30   21 : 16 : 57  org.apache.cxf.endpoint.ServerImpl initDestination
    None.gif信息: Setting the server's publish address to be /HelloWorld
    好了,那么样才能够看到wsdl文档呢?关键就在web.xml配置servlet那里,

    <
    servlet-mapping >
    None.gif        
    < servlet-name > CXFServlet </ servlet-name >
    None.gif        
    < url-pattern > /services/* </ url-pattern >
    </ servlet-mapping >
这个mapping里的<url-pattern>就是你的所有webservice的访问路径了,而在applicationContext-cxf.xml中定义的服务RUL是"/HelloWorld",你的应用服务是这样:http://localhost:8080/testProject/,那么上面的webservice访问路径就是http://localhost:8080/testProject/services/HelloWorld?wsdl。

转载于:https://www.cnblogs.com/TV9/archive/2008/06/30/1232726.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值