CXF实现WebService

一、CXF简介

Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。
CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。
特性:
支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。
Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL优先开发,也支持从 Java 的代码优先开发模式。
容易使用: CXF 设计得更加直观与容易使用。有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。
支持二进制和遗留协议:CXF 的设计是一种可插拨的架构,既可以支持 XML ,也可以支持非 XML 的类型绑定,比如:JSON 和 CORBA。
项目目标:
下面列出了来自 Apache CXF 官方网站的项目目标。
概要
高性能
可扩展
简单且容易使用
支持多种标准
支持 JAX-WS、 JAX-WSA、JSR-181 和 SAAJ;
支持 SOAP 1.1、1.2、WS-I BasicProfile、WS-Security、WS-Addressing、WS-RM 和 WS-Policy;
支持 WSDL 1.1 、2.0;
支持 MTOM;
多种传输方式、Bindings、Data Bindings 和 Format
Bindings:SOAP、REST/HTTP;
Data Bndings:目前支持 JAXB 2.0、Aegis 两种,默认是 JAXB 2.0。XMLBeans、Castor 和 JiBX 数据绑定方式将在 CXF 2.1 版本中得到支持;
格式(Format):XML、JSON;
传输方式:HTTP、Servlet、JMS 和 Jabber;
可扩展的 API 允许为 CXF 增加其它的 Bindings,以能够支持其它的消息格式,比如:CSV 和固定记录长度。
项目特点:
灵活部署
轻量级容器:可在 Tomcat 或基于 Spring 的容器中部署 Services;
集成 JBI:可以在如 ServiceMix, OpenESB or Petals 等等的 JBI 容器中将它部署为一个服务引擎;
集成 SCA:可以部署在如 Tuscany 之类的 SCA 容器中;
集成 J2EE:可以在 J2EE 应用服务器中部署 Services,比如:Geronimo、JOnAS、JBoss、WebSphere Application Server 和 WebLogic Application Server,以及 Jetty 和 Tomcat;
独立的 Java 客户端/服务器。
支持多种编程语言
全面支持 JAX-WS 2.0 客户端/服务器编程模型;
支持 JAX-WS 2.0 synchronous、asynchronous 和 one-way API’s;
支持 JAX-WS 2.0 Dynamic Invocation Interface (DII) API;
支持 wrapped and non-wrapped 风格;
支持 XML messaging API;
支持 JavaScript 和 ECMAScript 4 XML (E4X) ,客户端与服务端均支持;
通过 Yoko 支持 CORBA;
通过 Tuscany 支持 SCA;
通过 ServiceMix 支持 JBI ;
代码生成
Java to WSDL;
WSDL to Java;
XSD to WSDL;
WSDL to XML;
WSDL to SOAP;
WSDL to Service;
(来自百度百科)

二、CXF开发WebService代码实现

1、普通java项目代码实现

服务端开发:
(1)导入jar包。
maven导入:

<!-- cxf需要的jar包 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-core</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-bindings-soap</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-databinding-jaxb</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-simple</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.0.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-udp</artifactId>
			<version>3.0.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-ws-addr</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-wsdl</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.neethi</groupId>
			<artifactId>neethi</artifactId>
			<version>3.0.3</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.woodstox</groupId>
			<artifactId>stax2-api</artifactId>
			<version>3.1.4</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.woodstox</groupId>
			<artifactId>woodstox-core-asl</artifactId>
			<version>4.4.1</version>
		</dependency>
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
			<version>1.6.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.wss4j</groupId>
			<artifactId>wss4j-bindings</artifactId>
			<version>2.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.ws.xmlschema</groupId>
			<artifactId>xmlschema-core</artifactId>
			<version>2.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.0.2</version>
		</dependency>

手动导入:
可以去Apache CXF 官方网站下载CXF框架的安装包。CXF 框架目前的最新版本是 3.2.7,下载时请选择“二进制发布包(Binary distribution)”,感兴趣的话可以下载相应版本的“源代码发布包(Source distribution)”。下载后解压,然后配置环境变量 %CXF_HOME%(参照jdk环境变量配置)。jar包在lib目录下。手动导入下面图片中的几个jar包即可。
在这里插入图片描述
(2)创建服务接口SEI(Service Endpoint Interface),以及接口的实现类

这里可以参照我另外一篇关于webservice的博客:https://blog.csdn.net/qq_34609889/article/details/85338253

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

import com.dmf.sei.UserService;
/**
 * 
 * @author dmf
 * WebService 服务接口SEI(Service Endpoint Interface),在wsdl中就是portType
 *
 */
@WebService
//@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface UserService {

	public String saveUserInfo(String name,int age);
	public String getUserInfoById(String id);
}
@WebService    //@WebService注解表示该类是一个服务类,需要发布其中的public的方法
public class UserServiceImpl implements UserService{

	@Override
	public String saveUserInfo(String name, int age) {
		System.out.println("姓名:"+name+" 年龄:"+age);
		return "success";
	}

	@Override
	public String getUserInfoById(String id) {
		System.out.println("用户id:"+id);
		return "张三";
	}

}

(3)发布WebService服务

public class UserServicePublish {

	public static void main(String[] args) {
		UserService userService=new UserServiceImpl();
		JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean();
		
		//设置WebService服务地址
		bean.setAddress("http://localhost:9999/UserService");
		//设置WebService服务接口
		bean.setServiceClass(UserService.class);
		//设置WebService服务实现类
		bean.setServiceBean(userService);
		
		//添加输入拦截器  :输入显示日志信息的拦截器
		//bean.getInInterceptors().add(new LoggingInInterceptor());
		//添加输出拦截器  :输出显示日志信息的拦截器
		//bean.getOutInterceptors().add(new LoggingOutInterceptor());
		
		bean.create();//发布webservice
		System.out.println("webservice成功发布!");
	}
}

(4)测试WebService服务是否发布成功
在浏览器地址栏输入http://localhost:9999/UserService?wsdl。

在这里插入图片描述
(5)发布SOAP1.2的服务端
SOAP(简单对象访问协议)有1.1和1.2两个版本。使用最多的是1.1版本。如果想要发布SOAP1.2服务端,需要在接口上添加注解 @BindingType(SOAPBinding.SOAP12HTTP_BINDING)(接口注释部分)。
客户端开发
(1)生成代码,导入jar包。
上文我们说了怎么安装cxf框架,这里需要用到bin目录下的wsdl2java工具生成客户端代码,和jdk的wsimport工具类似。它的命令是wsdl2java。wsdl2java的常用参数:
-d,指定输出目录
-p,指定包名,如果不指定该参数,默认包名是WSDL的命名空间的倒序 。
Wsdl2java支持SOAP1.1和SOAP1.2。命令示例:
wsdl2java -d . http://localhost:9999/UserService?wsdl
.代表在当前目录生成代码
导入cxf需要的jar包
(2)编写测试类

public class CXFWebServiceClient {

	public static void main(String[] args) {
		//方法1
		/*UserServiceService service = new UserServiceService();
		UserService userService = service.getUserServicePort();
		
		String name = userService.getUserInfoById("123");
		System.out.println("根据id获取到的用户姓名为:"+name);*/
		
		//方法2
		JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
        //设置服务接口
        jaxWsProxyFactoryBean.setServiceClass(UserService.class); 
        //设置服务地址
        jaxWsProxyFactoryBean.setAddress("http://localhost:9999/UserService");
        //获取服务接口实例
        UserService userService=(UserService) jaxWsProxyFactoryBean.create();
        //调用方法
        String name = userService.getUserInfoById("123");
		System.out.println("根据id获取到的用户姓名为:"+name);
	}

2、CXF+Spring开发WebService

客户端开发
(1)导入Spring的依赖包。
maven导入:

<!-- Spring 依赖包 -->
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>

手动导入:
导入下列几个jar包即可
在这里插入图片描述
(2)创建SEI接口以及接口实现类(和普通java项目代码实现那节相同即可,这里就不复述了)。
(3)配置spring配置文件application.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <bean id="userServiceImpl" class="com.dmf.sei.impl.UserServiceImpl"/>
    
    <!--jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->
    <!--serviceClass属性是服务接口,address代表地址,serviceBean是服务实现类。-->
    <!--如果是web项目,address可以只配置服务名,例如 :/UserService。那么WebService访问地址就是http://ip:web项目端口/项目名/ws/UserService?wsdl-->
	<jaxws:server address="http://localhost:9999/UserService" serviceClass="com.dmf.sei.UserService">
		<jaxws:serviceBean>
			<ref bean="userServiceImpl" />
		</jaxws:serviceBean>
		<!-- 配置输入显示日志信息的拦截器,可以省略 -->
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:outInterceptors>
	</jaxws:server>
</beans>  

在这里插入图片描述
(4)发布WebService服务。

public class CXFSpringServicePublish {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

		System.out.println("webService启动成功!");
	}

}

客户端开发
(1)导入Spring的依赖jar包,以及使用wsdl2java生成客户端代码。
(2)配置spring的配置文件,application.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

  <!-- <jaxws:client实现客户端 ,对JaxWsProxyFactoryBean类封装-->   
    <!-- address是服务地址,servicelass是服务接口,返回服务实现类-->   
    <jaxws:client id="userService" address="http://localhost:9999/UserService" serviceClass="com.dmf.sei.UserService"/>
</beans>

(3)客户端调用服务

public class CXFSpringClient {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");

		UserService userService = (UserService) ctx.getBean("userService");
		// 调用方法
		String name = userService.getUserInfoById("123");
		System.out.println("根据id获取到的用户姓名为:" + name);

	}

}

web项目的CXF对应配置
服务端:
application.xml中jaxws:server标签的address可以配置为/UserService,无需加上ip端口,如果写了ip端口,则客户端按照这个配置来调用。

<jaxws:server address="/UserService" serviceClass="com.dmf.sei.UserService">

web.xml文件配置:

<!--配置Tomcat启动时加载Spring配置文件  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!--配置CXF提供的Servlet  -->
	<servlet>
		<servlet-name>CXF</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXF</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>

那么WebService访问地址就是http://ip:web项目端口/项目名/ws/UserService?wsdl
客户端:
application.xml中jaxws:client标签的address配置为:

<jaxws:client id="userService" address="http://localhost:8080/CXFWebServiceServer/ws/UserService" 
serviceClass="com.dmf.sei.UserService"/>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值