整合spring与cxf,利用cxf编写webservice

转自:http://www.cnblogs.com/modou/articles/1390645.html

一、下载cxf

下载地址:http://cxf.apache.org/download.html

我下载的是最新版的cxf 2.1.3

 

二、准备工作:在spring环境下使用cxf

1.下载完cxf的包后,至少需要添加如下包,才能正常使用cxf(我一个一个试的,很郁闷,居然要那么多,下载的包里都有):

cxf-2.1.3.jar

commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-jaxws_2.1_spec-1.0.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.7.jar
saaj-api-1.3.jar
wstx-asl-3.2.6.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.2.jar
xml-resolver-1.2.jar

 

2.添加如下内容到web.xml

    <!-- CXF WebService -->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>3</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

 

3.在spring配置文件中,修改文件内容为:

<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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

添加的内容有三部分:

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

②http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

③ <import resource="classpath:META-INF/cxf/cxf.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

可以在原有配置文件上修改

 

三、开始使用:编写服务端WebService

1.添加一个接口,Hello.java:

package com.test.webservice;

import javax.jws.WebService;

@WebService
public interface Hello {
    public String say(String text);
}

2.实现这个接口,HelloImpl.java:

package com.test.webservice.impl;

import javax.jws.WebService;

import com.ruiri.webservice.Hello;

@WebService(endpointInterface = "com.test.webservice.Hello")
public class HelloImpl implements Hello {

    public String say(String text) {
        System.out.println(text);
        return "Hello," + text;
    }

}

跟平时使用的接口和类一样,只是添加了对应的注解

3.在spring的配置文件添加如下内容:

    <jaxws:endpoint id="helloWorld" implementor="com.test.webservice.impl.HelloImpl"
        address="/HelloWorld" />

这样,服务端的WebService就做好了,在浏览器可以通过 http://站点/services/ 看到可见的service

 

四、编写客户端WebService

1.编写一个接口,这里还使用先前写的Hello.java

2.在spring的配置文件添加如下内容:

    <jaxws:client id="helloClient" serviceClass="com.ruiri.webservice.Test1"
        address="http://站点/services/HelloWorld" />

3.在代码用调用这个bean:

ServletContext sc = getServletContext();        
WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        Hello hello = (Hello) ac.getBean("helloClient");
        String str = hello.say("zhang");

这样,客户端调用的代码也有了,配置稍微有些麻烦,配置完后,使用倒是很简单。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值