加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 00:59:36 所属栏目:安全 来源:网络整理
导读:开发环境: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/cla
开发环境: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。

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读