WebService框架——CXF介绍

1. WebService与CXF简介

1.1 WebService

        WebService是一种跨编程语言和跨操作系统平台的远程调用技术。
        跨编程语言和跨操作平台 就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。
        远程调用 就是一台计算机a上的一个程序可以调用到另外一台计算机b上的一个对象的方法,譬如,银联提供给商场的pos刷卡系统,商场的POS机转账调用的转账方法的代码其实是跑在银行服务器上。再比如,amazon,天气预报系统,淘宝网,校内网,百度等把自己的系统服务以WebService服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能,这样扩展了自己系统的市场占有率。
服务端:把公司内部系统的业务方法发布成WebService服务,供远程他人调用
客户端:调用别人发布的WebService服务
常见的远程调动技术:
1)    Socket 套接字 TCP/IP UDP
2)    WebService
3)    http 调用
4)    RMI( 远程方法调用 ) Hessian 框架(二进制RPC协议传输数据)
WebService 的特点:
1)    跨平台,跨语言
2)    W3C(万维网联盟)制定的标准
3)    可以穿透防火墙(因为 soap 协议是基于 HTTP 协议)
SOAP 协议(简单对象访问协议Simple Object Access Protocol): 
        WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议
SOAP协议 = HTTP协议 + XML数据格式
        WSDL(Web Services Description Language)就是基于XML的语言,用于描述Web Service及其函数、参数和返回值。它是WebService客户端和服务器端都能理解的标准格式。因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。

1.2 CXF

        CXF,apache 下的 WebService 的开源框架。它支持多种协议,比如:SOAP1.1,1,2、XML/HTTP、REST HTTP 或者 CORBA。
灵活的部署:可以运行有 Tomcat,Jboss,weblogic,Jetty(内置)上面

2. CXF入门demo

2.1 服务端开发

2.1.1 工程搭建

1)引入CXF依赖

<dependency> 
	<groupId>org.apache.cxf</groupId> 
	<artifactId>cxf-rt-frontend-jaxws</artifactId> 
	<version>3.0.1</version> 
</dependency> 
<dependency> 
	<groupId>org.apache.cxf</groupId> 
	<artifactId>cxf-rt-transports-http</artifactId> 
	<version>3.0.1</version> 
</dependency>

2)引入Spring-web依赖

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>

3)web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  
  	<!-- 启动spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_cxf.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>


</web-app>

4)服务层代码编写

1. 接口

package cn.bjc.cxf.server;

import javax.jws.WebService;

/**天气服务接口
 * @author Administrator
 *
 */
@WebService
public interface IWeatherService {
	String info(String city);
}

2. 实现类

package cn.bjc.cxf.server.impl;

import cn.bjc.cxf.server.IWeatherService;

public class WeatherService implements IWeatherService {

	@Override
	public String info(String city) {
		return "北京".equals(city)?"雾霾":"晴天";
	}

}

4)spring配置文件applicationContext_cxf.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://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">

	<!-- WS调用的类 -->
	<bean id="weatherService" class="cn.bjc.cxf.server.impl.WeatherService"></bean>

	<!-- 发布服务 -->
	<jaxws:server address="/weatherService">
		<jaxws:serviceBean>
			<ref bean="weatherService" />
		</jaxws:serviceBean>
	</jaxws:server>
	
</beans>

5)运行

在浏览器输入网址:http://localhost:9090/cxf/ws/weatherService?wsdl 

如图,表示服务发布成功

这个内容就是 WSDL 文档,相当与 webservice 的使用说明书
我们可以看到这里还import了另一个xml,如下,

<wsdl:import location="http://localhost:9090/cxf/ws/weatherService?wsdl=IWeatherService.wsdl"

打开该链接,如图:

是一个WSDL文档内容

2.1.2 WSDL描述语言介绍

该文档我们怎么读了,乍一看很懵逼,仔细看又似乎有关联,我们需要从下往上读。

1)<wsdl:portType name="IWeatherService">

表示发布服务的接口,接口名为IWeatherService,该接口是不是很熟悉,就是上面我们写的接口的类名

1.1 <wsdl:operation name="info">

在portType标签下,有个子标签<wsdl:operation name="info">,表示方法,方法名叫info。该方法就是我们发布给外界调用的方法。

1.1.1 <wsdl:input message="ns1:info" name="info"></wsdl:input>

在operation标签下,有一个子标签,input,表示输入参数,name=info表示,该参数是info方法的参数

1.1.2 <wsdl:output message="ns1:infoResponse" name="infoResponse"></wsdl:output>

在operation标签下,有一个子标签,output,表示输出参数,infoResponse,表示方法的返回值

2)<xs:complexType name="info">

在根据方法名info,找到文档上方的complexType,name=info也表示方法名为info

子标签<xs:sequence>

表示输入参数列表,通过标签<xs:element minOccurs="0" name="arg0" type="xs:string"/>来表示,其中name为参数,用arg来表示,第一个参数用arg0,第二个用arg1,type=xs:String,表示参数类型为string类型。

3)<xs:complexType name="infoResponse">

根据输出参数infoResponse,找到标签<xs:complexType name="infoResponse">,name=infoResponse与output输出参数的name值对应。

子标签<xs:sequence>

表示输出参数,通过标签<xs:element minOccurs="0" name="return" type="xs:string"/>来表示,其中name=return表示该参数是返回数据,type=xs:string表示,返回值是string类型。

4)<xs:schema

<xs:schema中的targetNamespace="http://server.cxf.bjc.cn/"  ,就是我们的包名的倒写,如图;

整个文档,我们一般只关心收尾两个标签的内容,一个是wsdl:portType ,另一个是wsdl:types,如图:

这两部分内容,包含了丰富的接口信息,从中,我们可以直接接口的包名,接口名,方法,输入参数,输出参数信息。

2.2  客户端的开发

2.2.1 工程搭建

1)引入依赖

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>3.1.10</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>3.1.10</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.11</version>
	<scope>test</scope>
</dependency>

2.2.2 根据 WSDL 生成本地代码

1)打开cmd,进入工程目录,如图:

1. 复制目录

2. 打开cmd,命令行进入项目目录src/main/java目录下,如图:

2)运行命令

命令格式:

wsimport -s . 描述语言路径

参数解析;

wsimport:是java自带的一个工具

-s 表示生成的source代码 

. 点表示当前目录

例如:

刷新一下工程,可以看到工程多了如下目录代码,

2.2.3 spring配置客户端

<?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.xsd				           
				            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- 客户端配置 
		1. address:就是服务端发布的描述语言的路径
		2. serviceClass:生成的代码中的那个接口类,名称与portType中的名称一致
	-->
	<jaxws:client id="weatherClient" 
		address="http://localhost:9090/cxf/ws/weatherService?wsdl"
		serviceClass="cn.bjc.cxf.server.impl.IWeatherService"
	>
	</jaxws:client>
</beans>

2.2.4 编写测试用例

package cn.bjc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.bjc.cxf.server.impl.IWeatherService;

public class CxfTest {
	
	@Test
	public void test() {
		ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext_cxf.xml");
		IWeatherService bean = (IWeatherService) app.getBean("weatherClient");
		String info = bean.info("北京");
		System.out.println(info);
	}
	
}

输出结果;

3. SSM项目中使用CXF发布Webservice服务

3.1 在web工程中新建服务

1)接口

package cn.bjc.redsum.boss.wds;

import java.util.List;

import javax.jws.WebService;

import cn.bjc.redsum.boss.pojo.Waybilldetail;

/**webservice对外发布的接口
 * @author Administrator
 *
 */
@WebService
public interface IWaybillWds {
	
	/**根据运单号查询查询运单详细
	 * @param id
	 * @return
	 */
	public List<Waybilldetail> waybilldetailList(Long sn);
	
	/**在线预约下单
	 * @param userId        用户id
	 * @param toAddress		收货地址
	 * @param addressee		收货人
	 * @param tele			电话
	 * @param info			运单详情
	 * @return
	 */
	public Long addWaybill(Long userId,String toAddress,String addressee,String tele,String info);
	
}

2)实现类

package cn.bjc.redsum.boss.wds.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.bjc.redsum.boss.biz.IWaybillService;
import cn.bjc.redsum.boss.biz.IWaybilldetailService;
import cn.bjc.redsum.boss.pojo.Waybill;
import cn.bjc.redsum.boss.pojo.Waybilldetail;
import cn.bjc.redsum.boss.wds.IWaybillWds;

@Component
public class WaybillWds implements IWaybillWds {
	
	@Resource
	private IWaybilldetailService waybilldetailService;
	
	@Resource
	private IWaybillService waybillService;

	@Override
	public List<Waybilldetail> waybilldetailList(Long sn) {
		List<Waybilldetail> details = null;
		try {
			details = waybilldetailService.getDetailsBySn(sn);
		} catch (Exception e) {
			throw new RuntimeException("数据库异常,查询订单详情失败!", e);
		}
		return details;
	}

	@Override
	public Long addWaybill(Long userId, String toAddress, String addressee, String tele, String info) {
		checkParams(userId, toAddress, addressee, tele, info);
		Waybill ab = new Waybill();
		ab.setUserid(userId);
		ab.setToaddress(toAddress);
		ab.setAddressee(addressee);
		ab.setTele(tele);
		ab.setInfo(info);
		Long id = null;
		try {
			waybillService.save(ab);
		} catch (Exception e) {
			throw new RuntimeException("保存失败,数据库异常!",e);
		}
		return ab.getSn();
	}

	private void checkParams(Long userId, String toAddress, String addressee, String tele, String info) {
		if(null == userId) {
			throw new RuntimeException("保存失败,用户ID不能为空!");
		}
		if(StringUtils.isEmpty(toAddress)) {
			throw new RuntimeException("保存失败,收获地址不能为空!");
		}
		if(StringUtils.isEmpty(addressee)) {
			throw new RuntimeException("保存失败,收获人不能为空!");
		}
		if(StringUtils.isEmpty(tele)) {
			throw new RuntimeException("保存失败,联系电话不能为空!");
		}
		if(StringUtils.isEmpty(info)) {
			throw new RuntimeException("保存失败,运单详情不能为空!");
		}
	}

}

结构如图:

3.2 spring配置文件

1)cxf配置文件

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

	<!-- WS调用的类 -->
	<bean id="waybillWds" class="cn.bjc.redsum.boss.wds.impl.WaybillWds"></bean>

	<!-- 发布服务 -->
	<jaxws:server address="/waybillWds">
		<jaxws:serviceBean>
			<ref bean="waybillWds" />
		</jaxws:serviceBean>
	</jaxws:server>
	
</beans>

2)SpringMVC配置

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 启动自动扫描 -->
    <context:component-scan base-package="cn.bjc.redsum.boss" />

    <!-- 注册MVC注解驱动 -->
    <mvc:annotation-driven />

    <!-- 静态资源可访问的设置方式 -->
    <mvc:default-servlet-handler />

    <!-- 配置视图解析器,可以显式设置,也可以不设置,不设置会依据SpringMVC的默认设置 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

3.3 在web.xml中配置cfx过滤器

<!-- 加载cxf servlet -->
<filter>
	<filter-name>cxf</filter-name>
	<filter-class>org.apache.cxf.transport.servlet.CXFServlet</filter-class>
</filter>
<filter-mapping>
	<filter-name>cxf</filter-name>
	<url-pattern>/ws/*</url-pattern>
</filter-mapping>

当然了,还需要在Pom文件中引入cxf依赖

<!-- CXF依赖 -->
<dependency> 
	<groupId>org.apache.cxf</groupId> 
	<artifactId>cxf-rt-frontend-jaxws</artifactId> 
	<version>3.0.1</version> 
</dependency> 
<dependency> 
	<groupId>org.apache.cxf</groupId> 
	<artifactId>cxf-rt-transports-http</artifactId> 
	<version>3.0.1</version> 
</dependency>

3.4 发布服务

在浏览器输入网址:http://localhost:9090/redsum/ws/waybillWds?wsdl 结果如图:

表示发布成功。

这个发布的网址怎么来的了?

1)项目工程url:http://localhost"8080/redsum/

2)web.xml中配置的cxf过滤器的url-pattern:/ws

3)在cxf的配置文件中配置的address地址:/waybillWds

4)最后加上后缀 ?wsdl

所以,最后的发布连接就是:http://localhost:9090/redsum/ws/waybillWds?wsdl 

3.5 调用webservice服务

3.5.1 新建子工程client

在我们的maven中新建子工程client,然后,在maven工程的服务层中,添加client的依赖

3.5.2 生成代码

在client工程生成代码,操作步骤

1)复制路径 D:\erp\erp_parent\erp_client\src\main\java

2)打开控制台cmd,进入到我们的工程目录,如图:

3) 复制描述语言url:http://localhost:9090/redsum/ws/waybillWds?wsdl

4)在控制台输入如下命令:

wsimport -s . http://localhost:9090/redsum/ws/waybillWds?wsdl

回车,执行成功,如图:

5)刷新client工程,得到如图所示代码结构:

我们关注的代码部分就是impl包下的实现类

3.5.3 配置客户端client

1)引入依赖

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>3.1.10</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>3.1.10</version>
</dependency>

2)配置文件

<?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.xsd				           
				            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- 客户端配置 
		1. address:就是服务端发布的描述语言的路径
		2. serviceClass:生成的代码中的那个接口名
		注意;这里配置的是一个接口,跟我们之前配置的类不一样,这里不是实例化的意思,是表示应用这个接口
	-->
	<jaxws:client id="waybillClient" 
		address="http://localhost:9090/redsum/ws/waybillWds?wsdl"
		serviceClass="cn.bjc.redsum.boss.wds.impl.IWaybillWds"
	>
	</jaxws:client>
</beans>

3.5.4 调用

在我们的业务层(也可以是其他层),引入客户端,如图:

在需要使用到接口的地方调用即可,如图:

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值