WebService是一种跨编程语言、跨操作系统平台的远程调用技术,广泛应用在实际开发,接口实现,系统集成。

服务端

  1. List item

添加maven依赖
项目中除了spring相关的依赖以外,还需添加下面两个依赖。

    <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.3.5</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.3.5</version><scope>compile</scope></dependency>
  1. 配置web.xml

除了常规的spring相关配置,这里需要添加cxfServlet的配置。

<!--  1.cxfServlet配置--><servlet><servlet-name>cXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping><servlet-name>cXFServlet</servlet-name><url-pattern>/webservice/*</url-pattern></servlet-mapping>
<!--  2.配置spring容器--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value>
<!--    3.监听器--></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
  1. 编写webService接口以及实现类
    IHelloService 接口类
    接口需要标注@webservice注解
@WebService
public interface IHelloService {/***     对外发布服务接口的方法*      @param name*/public String welcome(String name);
}

HelloServiceImpl 接口实现类

public class HelloServiceImpl implements IHelloService {@Overridepublic String welcome(String name) {return "welcome to webService "+name;}
}
  1. 配置applicationContext.xml
    在命名空间中需要添加cxf相关的xsd,然后配置服务对应的信息,包括服务地址,服务类等
<?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:context="http://www.springframework.org/schema/context"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"><cxf:server address="/hello"><jaxws:serviceBean><bean class="service.impl.HelloServiceImpl"></bean></jaxws:serviceBean></cxf:server>
</beans>
  1. 启动web服务,测试
    服务启动成功后,我们可以在浏览器中访问:http://localhost:8081/webservice/hello?wsdl其中,http://localhost:8081为我们服务器访问入口,/webservice为我们在web.xml配置文件中配置的cxfServlet对应的servlet-mapping/hello为该服务访问的address地址,最后还需要加上?wsdl访问wsdl说明书。
    下图为ie浏览器的显示效果,我测试的火狐浏览器则显示空白,对应f12的内容与下图内容一致。
    客户端

  2. 添加cxf相关依赖

  3. 导入服务接口类型对应的类
    这里,我们需要将服务端的IHelloService和HelloServiceImpl添加到客户端代码中。另外,不管是客户端还是服务端,IHelloService接口都是需要标注@webService注解的。

  4. 编写客户端的applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"<jaxws:client id="helloService" serviceClass="service.impl.HelloServiceImpl" address="http://localhost:8081/webservice/hello"></jaxws:client>
</beans>
  1. 编写单元测试
    这里使用到了spring的单元测试,所以,不光要导入junit的依赖,还需要spring-test的依赖。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {//注入对象@Resourceprivate HelloServiceImpl helloService;@Testpublic void doClientRemote(){System.out.println(helloService);String content = helloService.welcome("Elaine");System.out.println(content);}}
  1. 查看输出结果
org.apache.cxf.jaxws.JaxWsClientProxy@45905bff
welcome to webService Elaine

可以发现,我们获取的HelloServiceImpl 对象是代理对象,因为事先了相关接口,自然是通过jdk动态代理实现的。
查看服务端输出时,有这样一个警告

org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://impl.service/}welcome found.   Expected {http://service/}welcome.

很好理解,我们在单元测试获取的代理对象类型为实现类,cxf更希望我们直接用接口类型。替换为:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {//注入对象@Resourceprivate IHelloService helloService;@Testpublic void doClientRemote(){System.out.println(helloService);String content = helloService.welcome("Elaine");System.out.println(content);}}

重新测试,无警告,无报错。

spring整合cxf,轻松编写webService客户端、服务端相关推荐

  1. Spring整合CXF,发布RSETful 风格WebService

    这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Spring整合CXF这里就不再多加赘述了.如果你对Spring整合CXF ...

  2. SpringBoot整合WebService(服务端+客户端)

    SpringBoot整合WebService(服务端+客户端) 文章目录 SpringBoot整合WebService(服务端+客户端) 一.服务端 1.项目结构 2.创建好SpringBoot项目后 ...

  3. 原神服务端搭建架设教程win系统(附客户端+服务端+环境配置)

    原神服务端搭建架设教程win系统(附客户端+服务端+环境配置) 大家好,我是艾西原神一款开放世界冒险3D游戏以七种元素(分别为风.雷.岩.火.水.草.冰)交汇的幻想世界"提瓦特"创 ...

  4. plsq卸载 删除注册表、_win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结...

    win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结 一:前提 注意:现在有两种安装的方式 1. oracle11g服务端(64位)+oracle客户端(32位)+plsql(3 ...

  5. 程序猿必须要知道的一个内容:客户端+服务端二(源码解析、建议收藏)

    客户端+服务端2(提升) 需先执行服务端,再执行客户端 package reflect;import java.io.File; import java.io.FileOutputStream; im ...

  6. 程序猿必须要知道的一个内容:客户端+服务端一(源码解析、建议收藏)

    客户端+服务端1(提升) 需要先开启服务端在开启客户端 package reflect;import java.io.ByteArrayOutputStream; import java.io.IOE ...

  7. java xmpp协议_GitHub - zhengzhi530/xmpp: 基于Xmpp协议的即时通讯社交软件(客户端+服务端)...

    yyquan 开源一个自己去年写的基于Xmpp协议的即时通讯社交软件 (客户端+服务端) 本项目仅供参考,对于正在学习Xmpp以及javaweb后台的同学,可以看一下. 做这个项目纯属个人兴趣爱好,所 ...

  8. 草帽船长(梦想海贼王)全套源码:客户端+服务端+资源+文档

    草帽船长(梦想海贼王)全套源码:客户端+服务端+资源+文档 ,需要帮助搭建联系QQ 2805477110 下载地址:http://www.51xyyx.com/2705.html 梦想海贼王全套源码, ...

  9. Node.js联机游戏——gobang五子棋(客户端+服务端+websocket的双人游戏)

    Node.js联机游戏--gobang五子棋(客户端+服务端+websocket的双人游戏) 五子棋的基本结构 ~·基本画图 ~·判断机制 ~···横向/竖向判断 ~···斜向判断 搭建服务器阶段 ~ ...

最新文章

  1. 数组专题——找重复数字 利用下标
  2. kaliLinux下保持匿名
  3. INI文件快速解析java工具包
  4. 时隔6年,NASA再造仿人机器人,或将在太空工作,应对严苛环境
  5. 从0开始编写dapper核心功能、压榨性能、自己动手丰衣足食
  6. coroutine php_PHP 协程实现
  7. 微信小程序srt_微信小程序微商城(八):缓存实现商品购物车功能
  8. orocod_kdl学习(一):坐标系变换
  9. Idea下的springboot mysql8.0等报错解决随笔
  10. jquery ajax中文编码设置
  11. python 编辑excel需要什么包,python操作excel的包(openpyxl、xlsxwriter)
  12. 易用宝项目记录day5-shiro
  13. lzg_ad:XPE常见问题FAQ
  14. NDB Cluster 基本介绍
  15. 学生宿舍管理系统c语言程序设计,学生宿舍管理系统C语言编程.doc
  16. 如何在win10自带的Edge浏览器中切换成IE浏览器浏览网站
  17. Linux管道命令及管道相关命令(详细)
  18. 机器人设计之一简单机械设计
  19. 【2021 CSP-J第二轮题解】
  20. 网页视频倍速播放的方法

热门文章

  1. Bash脚本和/ bin / bash ^ M:错误的解释器:没有这样的文件或目录[重复]
  2. 如何默认选择一个单选按钮? [重复]
  3. 如何删除旧的和未使用的Docker映像
  4. 如何使用bcrypt在PHP中对密码进行哈希处理?
  5. echo输出到stderr
  6. win11虚拟内存怎么修改 Windows11修改虚拟内存的步骤方法
  7. 图片放大缩小旋转左移右移镜像倒影android
  8. oracle asm和文件系统,Oracle技术之ASM迁移至文件系统
  9. JAVA:eclipse文本中文支持
  10. 洛谷——P2077 红绿灯(解法2)