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 {@Overridepublic 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 {@Testpublic 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 {@Resourceprivate IWaybilldetailService waybilldetailService;@Resourceprivate IWaybillService waybillService;@Overridepublic List<Waybilldetail> waybilldetailList(Long sn) {List<Waybilldetail> details = null;try {details = waybilldetailService.getDetailsBySn(sn);} catch (Exception e) {throw new RuntimeException("数据库异常,查询订单详情失败!", e);}return details;}@Overridepublic 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://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 调用

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

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

WebService框架——CXF介绍相关推荐

  1. 【WebService框架-CXF】——WebService和CXF

    在接下来的几篇博客中将对CXF的基础知识和简单应用进行总结.下面列出了要总结的内容.本篇文章集中介绍CXF的基础知识. CXF博客总结目录 WebService和CXF基础知识 CXF入门实例(编写客 ...

  2. 在spring中集成webservice 框架 CXF

    首先 构建环境 1.在eclipse下新建个web项目(我比较喜欢用eclipse),接着添加CXF必须依赖的jar包 commons-logging-1.1.jar  geronimo-activa ...

  3. 转载 WebService 的CXF框架 WS方式Spring开发

    WebService 的CXF框架 WS方式Spring开发 1.建项目,导包. 1 <project xmlns="http://maven.apache.org/POM/4.0.0 ...

  4. 几种流行Webservice框架性能对比

    1      摘要 开发webservice应用程序中离不开框架的支持,当open-open网站列举的就有30多种,这对于开发者如何选择带来一定的疑惑.性能Webservice的关键要素,不同的框架性 ...

  5. 几种流行Webservice框架性能对照

     转自[http://blog.csdn.net/thunder4393/article/details/5787121],写的非常好,以收藏. 1      摘要 开发webservice应用程序中 ...

  6. Java CXF介绍与实例

    CXF简介 CXF是一个Java 版的Web Service框架 CXF是由过去的Celtix和XFire两个框架合并而来,CXF在java社区有广泛的接受度是得益于它能很好的集成Spring. CX ...

  7. 关于webservice(CXF)的一些理解

    CXF是apache下开源的webservice框架,CXF依赖spring进行集成,支持soap1.1,soap1.2,XML/HTTP,RESTful HTTP 或者CORBA; 使用jaxws来 ...

  8. WebService与CXF

    WebService简介 Webservice也叫XML Web Service,Web服务.可使用开放的xml标准来描述.发布.发现.协调和配置这些应用程序.用于开发分布式的互操作的应用程序.是一种 ...

  9. 转 真正的轻量级WebService框架——使用JAX-WS(JWS)发布WebService

    WebService历来都很受重视,特别是Java阵营,WebService框架和技术层出不穷.知名的XFile(新的如CXF).Axis1.Axis2等. 而Sun公司也不甘落后,从早期的JAX-R ...

  10. NET Core微服务之路:自己动手实现Rpc服务框架,基于DotEasy.Rpc服务框架的介绍和集成...

    原文:NET Core微服务之路:自己动手实现Rpc服务框架,基于DotEasy.Rpc服务框架的介绍和集成 本篇内容属于非实用性(拿来即用)介绍,如对框架设计没兴趣的朋友,请略过. 快一个月没有写博 ...

最新文章

  1. Tungsten Fabric SDN — Orchestrator 集成部署模式 — with Kubernetes
  2. Windows使用opencv训练模型过程记录(提供样本)
  3. MySQL 5.6 for Windows 解压缩版配置安装
  4. opencv10-形态学操作
  5. 后台模板 开源_3个开源样板网页设计模板
  6. .net backend return json string , used by frontend
  7. php中svn上传项目直接访问不了,phpstorm8 通过svn导入项目后项目右键列表里没有subversion选项,无法提交和更新啊?...
  8. java类对象的内部结构图解(java对象模型精讲)
  9. Hover.css:一组超实用的 CSS3 悬停效果和动画
  10. 原生安卓10怎么打开面部识别_安卓手机运行慢怎么办?只需简单一步立即提速翻倍...
  11. Beta冲刺Day4
  12. Deep3DFaceReconstruction让一张人脸照片变成三维的真人脸
  13. 回看科技股泡沫:区块链崛起恰逢其时,相当于1996年的互联网
  14. 手把手教你电机FOC控制【二】
  15. 分享一款上班摸鱼神器,再也不怕领导突然出现在身后了~
  16. 洛谷P8255 解法 2020328
  17. 【全局规划】人工势场法(APF)
  18. Nuxt.js理解 开启SSR渲染(介绍)---00
  19. redis之lua脚本: 原子性 调试 嵌入高级语言
  20. 1.7编程基础字符串11潜伏者

热门文章

  1. r语言平均值显著性检验_R语言相关系数、显著性检验及可视化的尝试
  2. sci论文 计算机,计算机类SCI论文
  3. 电脑网页截屏怎么截长图?借助Safari对整个网页进行长截图
  4. 静态路由配置实例学习记录
  5. KARL MAYER卡尔迈耶驱动器维修SP0405-KM SP0404 SP0403
  6. viper4android还用酷狗,听HiFi专业的App,VIPER独占USB源码输出发烧友用过都上瘾
  7. python 图标字体_使用nerd-font/font-patcher为字体添加字体图标
  8. 高颜值智能存储 华三魔术家M2无线云盘评测
  9. 数模电路基础知识 —— 4. 常见电路符号说明(二极管)
  10. robots.txt详解