您的位置:首页 > 编程语言 > Java开发

CXF+Spring 实现WebService

2013-12-26 16:53 197 查看

1.概述

CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code
First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

2.功能特性

CXF 包含了大量的功能特性,但是主要集中在以下几个方面:

1.支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。

2.Frontends:
CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL 优先开发,也支持从 Java 的代码优先开发模式。

3.容易使用: CXF 设计得更加直观与容易使用。有大量简单的 API 用来快速地构建代码优先的Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。

4.支持二进制和遗留协议:CXF 的设计是一种可插拨的架构,既可以支持 XML ,也可以支持非XML 的类型绑定,比如:JSON 和 CORBA。

5.支持多种标准:

a.支持 JAX-WS、 JAX-WSA、JSR-181 和 SAAJ;

b.支持 SOAP 1.1、1.2、WS-I BasicProfile、WS-Security、WS-Addressing、WS-RM 和 WS-Policy;

c.支持 WSDL 1.1 、2.0;

d.支持 MTOM;

6.多种传输方式、Bindings、Data Bindings 和 Format:

a.Bindings:SOAP、REST/HTTP;

b.Data Bndings:目前支持 JAXB 2.0、Aegis 两种,默认是 JAXB 2.0。XMLBeans、Castor和 JiBX 数据绑定方式将在 CXF 2.1 版本中得到支持;

c.格式(Format):XML、JSON;

d.传输方式:HTTP、Servlet、JMS 和 Jabber;

e.可扩展的 API 允许为 CXF 增加其它的 Bindings,以能够支持其它的消息格式,比如:CSV 和固定记录长度。

7.灵活部署

a.轻量级容器:可在 Tomcat 或基于 Spring 的容器中部署 Services;

b.集成 JBI:可以在如 ServiceMix, OpenESB or Petals 等等的 JBI 容器中将它部署为一个服务引擎;

c.集成 SCA:可以部署在如 Tuscany 之类的 SCA 容器中;

d.集成 J2EE:可以在 J2EE 应用服务器中部署 Services,比如:Geronimo、JOnAS、JBoss、WebSphere Application Server 和 WebLogic Application Server,以及 Jetty 和 Tomcat;

e.独立的 Java 客户端/服务器。

8.支持多种编程语言

a.全面支持 JAX-WS 2.0 客户端/服务器编程模型;

b.支持 JAX-WS 2.0 synchronous、asynchronous 和 one-way API's;

c.支持 JAX-WS 2.0 Dynamic Invocation Interface (DII) API;

d.支持 wrapped and non-wrapped 风格;

e.支持 XML messaging API;

f.支持 JavaScript 和 ECMAScript 4 XML (E4X) ,客户端与服务端均支持;

g通过 Yoko 支持 CORBA;

h.通过 Tuscany 支持 SCA;

i.通过 ServiceMix 支持 JBI ;

9.代码生成

a.Java to WSDL;

b.WSDL to Java;

c.XSD to WSDL;

d.WSDL to XML;

e.WSDL to SOAP;

f.WSDL to Service;

3.轻松与Spring集成

3.1服务端
1.定义WebService所需暴露的接口:接口中只有一个方法public String vote,只需在接口定义处添加@WebService注明此接口将要提供服务.
package ws.cxf;

import javax.jws.WebService;

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


2.编写接口实现类:vote方法中判断username入参是否为Michael,如果是将返回重复投票

package ws.cxf.impl;

import ws.cxf.ISurveyService;

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;
	}

}


3.服务器端配置文件:需要注意两个地方,将CXF所需的配置文件import到Spring容器中,但所需的3个配置文件并不在工程里,而是在Spring容器初始化时自动创建,这里需要显示的将其import进来;

定义了一个Webservice的端点,其中id是其存放在Spring容器中的标识, implementor用于标注实现类, address提供服务的地址.



4.web.xml配置:在web.xml中只需定义Spring容器的监听器,CXFServlet和CXFServlet mapping



3.2客户端

1.客户端配置文件(仅供客户端使用Spring框架):
和服务器端得配置一样,也需要import相关CXF配置文件到Spring容器;同时还要使用定义客户端的bean,其中id为其存在Spring容器中的标识, serviceClass为WebService暴露接口的完全限定名, address为提供服务的地址.



2.客户端调用(使用Spring容器):只需创建Sping容器根据ID获取ISurveyService并调用所需方法
public static void main(String[] args){
		System.out.println(SurveyServiceClient.doSpringClient("Funny",88));
	//	System.out.println(SurveyServiceClient.doNormalClient("Michael",90));
	}
	private static String doSpringClient(String username,int point){
		// 加载客户端的配置定义
		ApplicationContext context = 
			new FileSystemXmlApplicationContext("src/test/ws/cxf/client/beanRefClient.xml");
		// 获取定义的 Web Service Bean
		ISurveyService surveyService = (ISurveyService)context.getBean("surveyServiceClient");
		// 调用方法进行服务消费
		return surveyService.vote(username,point);
	}


3. 客户端调用(java语言客户端):初始化代理工厂并将接口类型和服务地址设置近工厂类,就可获得所需服务

private static String doNormalClient(String username,int point){
		//初始化代理工厂
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		//定义服务接口类型
        factory.setServiceClass(ISurveyService.class);
        //设置服务地址
        factory.setAddress
        	("http://localhost:8080/CXF_Spring_Survey/SurveyWebService");
        ISurveyService client = (ISurveyService) factory.create();
	    return client.vote(username,point);
	}


3.3WSDL












4.源码下载

转载请标明出处:http://blog.csdn.net/shimiso

欢迎有识之士加入我们的技术交流群:173711587
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: