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

CXF,webservice开发、配置发布备忘

开发环境:Eclipse indigo + Tomcat6.0 + JDK1.6 + CXF2.5


1、Eclipse新建一个Dynamic Web Project工程,创建时即可以选择运行环境,如Tomcat6.0、CXF2.5。
建好后在工程属性Properties->Java Build Path->Source->Default output folder里把:项目名/build/classes修改为:项目名/WebContent/WEB-INF/classes(只是习惯,也可不改)


2、创建后会包含所有CXF库,如果创建时没有配置、选择CXF,可以从cxf文件夹下的lib文件夹中复制cxf.jar、spring-core.jar、spring-web.jar、spring-beans.jar、spring-context.jar、commons-logging.jar、fastinfoset.jar、needhi.jar、wsdl4j.jar、XmlSchema.jar,在Eclipse->CxfTest->Webroot->WEB-INF->lib点击右键选择粘贴(当然,也可以直接复制到该目录下然后刷新lib)

3、编辑web.xml

  1. 在web.xml文件添加以下servlet配置:
  2. 复制代码

     <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>WEB-INF/service-beans.xml</param-value> //指定配置文件
     </context-param>
     <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>


        
    < servlet >

            
    servlet-name CXFServlet </ servlet-class org.apache.cxf.transport.servlet.CXFServlet load-on-startup 1 servlet-mapping url-pattern /services/* >

    复制代码


  3. 添加service-beans.xml文件到上下文,配置如下:

    复制代码

    <? xml version="1.0" encoding="UTF-8" ?>

    beans  xmlns ="http://www.springframework.org/schema/beans"

        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:jaxws
    ="http://cxf.apache.org/jaxws"

        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"

        default-autowire
    ="byName"  default-lazy-init ="true" description 基于Apache CXF的Web Service配置文件

        

        
    import  resource ="classpath:meta-inf/cxf/cxf.xml" /> ="classpath:meta-inf/cxf/cxf-servlet.xml" ="classpath:meta-inf/cxf/cxf-extension-soap.xml" import

            
    ="classpath:meta-inf/cxf/cxf-extension-javascript-client.xml" bean  id ="helloWorldImpl"  class ="com.lbg.ws.test.impl.HelloWorld" jaxws:endpoint  ="helloWorld"  implementor ="#helloWorldImpl"

            address
    ="/HelloWorld" beans

    复制代码


  • 建立webservice的接口与实现:
    package  com.lbg.ws.test;


    import  javax.jws.WebMethod;

     javax.jws.WebService;


    @WebService(name
    = " HelloWorld )

    public   interface  IHelloWorld  {


        @WebMethod(operationName
    ="sayHello)

        
    public String sayHello();

    }

    复制代码

    package  com.lbg.ws.test.impl;


    import  javax.jws.WebService;


    import  com.lbg.ws.test.IHelloWorld;


    @WebService(endpointInterface
    = " com.lbg.ws.test.IHelloWorld " )

    public   class  HelloWorld  implements  IHelloWorld  {


        
    public String sayHello() {

            
    return "HelloWorld!";

        }

    }

    复制代码

    复制代码

  • 注意:@WebService表示该接口是一个WebService服务,@WebResult(name="response")表示在返回的SOAP消息中回应的标签名称;如果sayHello有参数,则需要使用@WebParam(name="param"),需包含 import javax.jws.WebParam
  • 把项目部署到tomcat或其它j2ee容器上启动,成功信息如下:

    复制代码

    2008 - 6 - 30   21 : 16 : 57  org.apache.cxf.service.factory.ReflectionServicefactorybean buildServiceFromClass

    信息: Creating Service {http://impl.test.ws.lbg.com/}HelloWorldService from class com.lbg.ws.test.IHelloWorld

    2008 - 6 - 30   21 : 16 : 57  org.apache.cxf.endpoint.ServerImpl initDestination

    信息: Setting the server's publish address to be /HelloWorld
    好了,那么样才能够看到wsdl文档呢?关键就在web.xml配置servlet那里,

    <
    >

            
    > CXFServlet > /services/* >

    复制代码

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

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

    相关推荐