【转】Apache CXF 简介

Apache CXF 简介

开放源代码的服务框架

 

简介:  本教程介绍了 Apache CXF 服务框架的基本知识,并通过讲解自带的例子来初步体验通过 CXF 进行服务的发布与消费;然后搭建基于 Eclipse 的 Apache CXF 开发环境,并通过一个“调查投票”示例应用程序来演示 CXF 整合 Spring 2.0 的基本开发过程。

CXF 应用开发

下面就将开始我们的 CXF Web Services 的开发之旅!首先,要有一个基于 Eclipse 的开发环境;然后,我们将利用这个开发环境开发一个简单的“调查投票”示例,同时我们将解释一些 CXF 在开发中进行配置的基本方法。

开发环境准备

在开发之前我们通常需要做些准备工作,比如搭建好开发环境。在本教程中,我们将在 Eclipse 3.2 环境中开发,当然如果您是使用 Eclipse Europa(Eclipse 3.3)也是可以的,我们假设您已经下载并且在计算机中安装好了 Eclipse 开发环境(关于如何下载和安装 Eclipse,请参见 参考资源)。


创建项目骨架

启动 Eclipse,创建一个 Java Project,如果是 WTP 的话,可以直接创建一个 J2EE 的 Web 项目,我们取名为 CXF_Spring_Survey,并设置编译的 output 路径为 WEB-INF/classes 目录,方便直接部署应用程序。

目录结构如下图所示:


图 4. 利用 CXF 开发 Web Services 的工程骨架示意图
图 4. 利用 CXF 开发 Web Services 的工程骨架示意图

为了方便起见,我们直接拷贝 %CXF_HOME%/lib 目录下的所有 .jar 文件到 CXF_Spring_Survey 项目的 WEB-INF/lib 目录下,也可以根据前面“CXF 安装包”章节所述的各个 jar 包的作用范围选择仅需要的 .jar 文件。在 Eclipse 里刷新后,可以看到如下结构:


图 5. 利用 CXF 开发 Web Services 的工程引入所有 .jar 文件后的骨架示意图
图 5. 利用 CXF 开发 Web Services 的工程引入所有 .jar 文件后的骨架示意图

并在 CXF_Spring_Survey 项目属性里将这些 .jar 加到 Java Build Path 当中去,如下图:


图 6. Eclipse 中引入所有 .jar 文件后的示意图
图 6. Eclipse 中引入所有 .jar 文件后的示意图

这样,项目的基本骨架已经创建完成,接下来开始编写接口与具体实现的代码了。


接口类创建

在项目的 src 目录中新建一个 ws.cxf 包,并在里面创建接口类 ISurveyService.java,为了简单示示例起见,我们仅创建一个方法 public String vote(String username,int point); 这里要注意的是我们在接口上用 @WebService 注解标明这是一个即将暴露为 Web Service 的接口,并将里面的方法都暴露出去。完整的接口代码清单如下:

package ws.cxf;

import javax.jws.WebService;

@WebService
public interface ISurveyService
{
	/**
	 * @param username 名字
	 * @param point 分数
	 * @return
	 */
	public String vote(String username,int point);
}

接下来,我们根据接口的定义,来实现它。


具体类实现

针对接口的定义,我们创建一个相应的实现类,并将其定义在 sw.cxf.impl 包中,完整的代码清单如下:

package ws.cxf.impl;

import javax.jws.WebService;
import ws.cxf.ISurveyService;

@WebService
public class SurveyService implements ISurveyService
{
	private String excludeName = "Michael";
	private int leastPonit = 5;

	public String vote(String username,int point)
	{
		String result = "";
		if(excludeName.equals(username))
		{
			result = " 您不能重复进行投票!";
		}
		else
		{
			result = " 谢谢您的投票!";
			if(point < leastPonit)
			{
				result += " 您的投票分数太低!";
			}
			else
			{
				result += " 您的投票分数通过审核!";
			}
		}
		return result;
	}

	// For IoC
	public String getExcludeName()
	{
		return excludeName;
	}

	public void setExcludeName(String excludeName)
	{
		this.excludeName = excludeName;
	}

	public int getLeastPonit()
	{
		return leastPonit;
	}

	public void setLeastPonit(int leastPonit)
	{
		this.leastPonit = leastPonit;
	}
}

接口定义与具体的实现就这样简单完成了,接下来就是相关的配置工作了,首先进行 Spring 的 Bean 配置。


Spring 配置

在 src 目录中创建 beanRefServer.xml 文件,用来定义 Spring 的 Bean 的配置,CXF 支持 Spring 2.0 Schema 标签配置方式,并且提供快捷暴露 Web Services 的标签。

首先,我们需要引入 Spring 与 CXF 的命名空间(namespace),如下:

<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-2.0.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd">

这样,我们可以使用 Spring 与 CXF 的标签配置了。接着,我们需要引入我们所需要的 CXF 的 Bean 定义文件,如下:

    <!-- Import Apache CXF Bean Definition -->
    <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"/>

接着定义我们具体实现的 Bean ,这个 Bean 的定义与 Spring 普通的 Bean 定义是一样的:

    <!-- SurveyService -->
    <bean id="surveyService" class="ws.cxf.impl.SurveyService">
        <property name="excludeName" value="Michael"/>
        <property name="leastPonit" value="10"/>
    </bean>

最后,将定义的 Bean 暴露出去成为 Web Service 服务,通过 CXF 提供的 Schema 标签配置 <jaxws:server> ,这样定义的配置显得更加简洁与方便,定义如下:

    <!-- Expose SurveyWebService -->
    <jaxws:server id="surveyWebService" 
        serviceClass="ws.cxf.ISurveyService" 
        address="/SurveyWebService">
        <jaxws:serviceBean>
            <ref bean="surveyService"/> <!-- 要暴露的 bean 的引用 -->
            </jaxws:serviceBean>
    </jaxws:server>

在配置中,serviceClass 的值是我们的接口类的名称,address 为将要暴露出去的 Web Service 访问地址。比如:/SurveyWebService,那么客户端消费 Web Service 的地址就会成为 http://host:port/WebAPPName/SurveyWebService ,与之相应的 WSDL 地址则为: http://host:port/WebAPPName/SurveyWebService?wsdl 。


Web 应用配置

由于我们的示例是需要通过 Servlet 容器进行服务暴露,因此需要配置相对应的 web.xml 文件,首先是增加 Spring 的配置文件加载 Listener,如下:

    <!-- Spring Config Location -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/beanRefServer.xml</param-value>
    </context-param>
    <!-- Spring ContextLoaderListener -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

接下来配置 CXF Servlet 的定义,以及它的映射,如下:

    <!-- Apache CXFServlet -->
    <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>1</load-on-startup>
    </servlet>
    <!-- CXFServlet Mapping -->
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

我们将之映射为 /* 。这样,服务端的代码与配置就全部完成了,接下来就是将应用程序部署到 Web 容器中去,并验证服务是否正常发布。


应用部署

我们将应用部署到 Tomcat 5.5.25 当中,在这里,我们采用链接(Link)的部署方式,简单而方便,在 %TOMCAT_HOME%/conf/Catalina/localhost/ 目录下创建与项目名称 CXF_Spring_Survey 一致的 xml 文件:CXF_Spring_Survey.xml,内容为:

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="F:/JavaProject/WebService/CXF/CXF_Spring_Survey"/>

docBase 里的内容根据您的实际项目所在的目录进行更改,注意使用 / 而不是 \ 即可。


启动服务

这时开始启动 Tomcat ,在启动的过程中,可以在启动窗口上看到以链接方式部署的应用在启动中会打印出一些相关信息来,最后显示启动成功。通过访问 http://localhost:8080/CXF_Spring_Survey/ 可以看到 CXF 暴露的服务链接:


图 7. CXF 暴露的服务链接的内容示意图
图 7. CXF 暴露的服务链接的内容示意图

可以直接点击进去,或者手工输入 WSDL 的地址进行访问:http://localhost:8080/CXF_Spring_Survey/SurveyWebService?wsdl ,可以看到如下的 WSDL 内容:


图 8. SurveyWebService 的 WSDL 内容示意图
图 8. SurveyWebService 的 WSDL 内容示意图

这样,我们可以确定我们的服务真正发布成功了,接下来就可以利用客户端进行消费了。


消费服务

回到 Eclipse 开发平台,开始编写消费服务相关的代码,首先通过 Spring 与 CXF 的配置来定义 Web Service 的客户端 Bean,在 src 目录下创建 beanRefClient.xml 配置文件,同样,我们也需要引入 Spring 与 CXF 命名空间的声明,并引入 CXF 的 Bean 的定义文件,最后通过与服务端配置相对的 CXF 标签 <jaxws:client> 来定义客户端访问服务的声明,完整的定义内容如下:

<?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://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd">
    <!-- Import Apache CXF Bean Definition -->
    <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"/>

    <!-- SurveyWebService Client -->
    <jaxws:client id="surveyServiceClient" 
        serviceClass="ws.cxf.ISurveyService" 
        address="http://localhost:8080/CXF_Spring_Survey/SurveyWebService"/>
</beans>

定义说明:id 为 Spring 定义的 id,用来在程序里进行获取它的标识,serviceClass 仍是为服务端定义的接口类,address 为完整的 Web Service 地址,这个与服务端的定义不一样。

定义完配置文件,接下来我们编写访问的具体代码,在 test 目录下创建 ws.cxf.client 包,然后创建 SurveyServiceClient.java,完整的代码如下:

package ws.cxf.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ws.cxf.ISurveyService;

public class SurveyServiceClient
{
	public static void main(String[] args)
	{
		// 加载客户端的配置定义
		ApplicationContext context = new 
				ClassPathXmlApplicationContext("beanRefClient.xml");
		// 获取定义的 Web Service Bean
		ISurveyService surveyService = 
			(ISurveyService)context.getBean("surveyServiceClient");
		// 1、定义调查投票的变量与内容,用来发送给服务
		String username = "Test";
		int point = 88;
		// 调用方法进行服务消费
		String result = surveyService.vote(username,point);
		System.out.println("Result:" + result);
		// 2、传递不一样的调查投票内容
		username = "Michael";
		point = 100;
		// 再次调用方法进行服务消费,得到不一样的结果
		result = surveyService.vote(username,point);
		System.out.println("Result:" + result);
		// 3、第三次传递与调用
		username = "Jordan";
		point = 9;
		result = surveyService.vote(username,point);
		System.out.println("Result:" + result);
	}
}

直接运行以上客户端消费程序,一共调用了三次 Web Service,并得到结果如下:

Result: 谢谢您的投票!您的投票分数通过审核!
Result: 您不能重复进行投票!
Result: 谢谢您的投票!您的投票分数太低!

于是服务正常地得到了调用,并且能够正确地返回结果,完整的代码及配置文件可以在本教程的 下载链接 里进行下载。

posted @ 2011-03-23 14:23  blueKnight  Views(1004)  Comments(0Edit  收藏  举报