[webservice] springboot整合cxf

news/2025/2/2 0:46:24/

1. cxf是什么

    Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的交互操作的应用程序。Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。CXF,Apache CXF = Celtix +XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF,继承了 Celtix 和 XFire 两大开源项目的精华,提供了对JAX-WS全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。

2. springboot整合cxf的操作(server端发布)

2.1 pom.xml 中添加依赖

springboot整合Apache cxf

       <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.5.7</version><exclusions><!-- CXF uses java.util.logging by default --><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId><version>2.3.12.RELEASE</version><exclusions><exclusion><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.3</version></dependency><!-- dom4j解析xpath依赖 --><dependency><groupId>jaxen</groupId><artifactId>jaxen</artifactId><version>2.0.0</version></dependency>

2.2 编写server端

@Service
// 配置targetNamespace和服务名称
@WebService(targetNamespace = "ws.vh.com", name = "MedicalOrderSvc")
public class MedicalOrderService {// 配置service中的method@WebMethod(operationName = "medicationOrderRequest")@WebResult// 使用WebParam配置参数名称,不配置的话默认会是arg0之类的名称public R medicationOrderRequest(@WebParam(name = "message") String message) { return null;}
}

2.3 service发布(cxf配置)

@Configuration
@EnableWs
public class CxfConfig {/*** 注意:此方法被注释后WSDL访问地址是http://127.0.0.1:8080/services/helloService?wsdl* 放开注释后WSDL访问地址是:http://127.0.0.1:8080/ws/helloService?wsdl*/@Beanpublic ServletRegistrationBean wsServletConfig() {return new ServletRegistrationBean(new CXFServlet(), "/ws/*");}@Resourceprivate MedicalOrderService medicalOrderService;// 对于多个发布的service,可以写多个bean来实现@Beanpublic Endpoint orderEndpoint() {return formatEndpoint("/medicalOrderService", medicalOrderService);}/*** 格式化暴漏的ws接口信息** @param path  暴露的ws请求地址* @param svc   与地址对应的ws服务* @return*/@ResourceBus bus;private Endpoint formatEndpoint(String path, Object svc) {EndpointImpl endpoint = new EndpointImpl(bus, svc);endpoint.publish(path);return endpoint;}
}

发布完成后启动web服务,可以通过:http://127.0.0.1:8080/ws 访问已经发布的服务。
在这里分享一个天气预报的webservice服务:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

3. webservice使用(client端调用)

3.1 使用wsdl2java生成webservice客户端代码

将目录切换到下载的apache-cxf-版本文件包的bin目录下,然后执行如下命令wsdl2java -p com.vh -d /Users/johnny/Desktop/aa -client http://127.0.0.1:8083/ws/medicalOrderService?wsdl

  • -p:指定生成的Java代码的package
  • -d:指定生产的文件的根目录
  • -client 指定线上或本地的wsdl文件,用于生成客户端代码

3.2 使用

   @DisplayName("临时测试使用")@Testpublic void test1() {MedicalOrderServiceService svc = new MedicalOrderServiceService();svc.getMedicalOrderSvcPort().medicationOrderRequest(message);}

3.3 xpath说明

对于XML文档的读取来说,xpath几乎是通常的选择,而在使用webservice时通常伴随着对于XML文档的解析。
xpath常用规则(其它规则可以参考文档:https://blog.csdn.net/qq_44619675/article/details/113938171)

表达式描述
nodename选取此节点的所有子节点
/从根节点选取直接子节点(相当于绝对路径)
//从当前节点选择子节点(相当于相对路径)
.选取当前节点
选取当前节点的父节点
@选取属性
以上面天气预报为例使用dom4j+xpath读取第一个方法的描述
@Test@DisplayName("读取天气预报wsdl第一个方法的描述")public void test2() throws IOException, DocumentException {URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");URLConnection conn = url.openConnection();SAXReader saxReader = new SAXReader();Document doc = saxReader.read(conn.getInputStream());Node node = doc.selectSingleNode("/wsdl:definitions/wsdl:portType[@name=\"WeatherWebServiceSoap\"]/wsdl:operation[@name=\"getSupportCity\"]/wsdl:documentation");System.out.println(node.getText());}

4. 参考文档

  • https://cxf.apache.org/docs/index.html
  • https://baike.baidu.com/item/Web%20Service/1215039?fr=ge_ala
  • https://blog.csdn.net/qq_44619675/article/details/113938171

http://www.ppmy.cn/news/1226943.html

相关文章

Qt调起Mac“系统设置”面板

mac系统设置相关字段&#xff1a; Accessibility 面板相关 项目 URL Scheme Main x-apple.systempreferences:com.apple.preference.universalaccess Display x-apple.systempreferences:com.apple.preference.universalaccess?Seeing_Display Zoom x-apple.systempreference…

ubuntu22.04换源

1、系统信息 lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.3 LTS Release: 22.04 Codename: jammy2、进入 /etc/apt/ 目录&#xff1a; cd /etc/apt/ 3、备份默认源文件 sudo cp sources.list sources.list_bak 4、编…

谈谈 MySQL 事务隔离级别

程序员的公众号&#xff1a;源1024&#xff0c;获取更多资料&#xff0c;无加密无套路&#xff01; 最近整理了一份大厂面试资料《史上最全大厂面试题》&#xff0c;Springboot、微服务、算法、数据结构、Zookeeper、Mybatis、Dubbo、linux、Kafka、Elasticsearch、数据库等等 …

消息消费过程

前言 本文介绍下Kafka消费过程, 内容涉及消费与消费组, 主题与分区, 位移提交&#xff0c;分区再平衡和消费者拦截器等内容。 消费者与消费组 Kafka将消费者组织为消费组, 消息只会被投递给消费组中的1个消费者。因此, 从不同消费组中的消费者来看, Kafka是多播(Pub/Sub)模式…

springboot+activiti5.22.0集成Activiti在线流程设计器

SpringBoot集成Activiti5.22在线流程设计器 文章目录 SpringBoot集成Activiti5.22在线流程设计器&#x1f4dd;1.增加配置pom依赖 增加数据库及redis配置文件&#x1f4dc; 2.启动类ActivitiDesignApplication排除安全校验注解启动项目后将会自动在数据库中生成表 &#x1f4d8…

Rust7.2 More About Cargo and Crates.io

Rust学习笔记 Rust编程语言入门教程课程笔记 参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community) Lecture 14: More About Cargo and Crates.io main.rs //Customizing Builds with Release Pro…

用5000字讲清楚压敏电阻

大家好,这里是大话硬件。 今天这篇文章用5000字的篇幅讲清楚压敏电阻。 1. 压敏电阻 压敏电阻,英文名Voltage Dependent Resistor,缩写VDR,或者叫Varistor,Variable(会变的)+ Resistor(电阻)。它的伏安特性曲线具有非线性。也就是压敏电阻的阻值并不是固定的,存在…

数据库mysql详细教学

目录 mysql的第一组基本操作&#xff1a;数据库操作 1、查看当前数据库 2、创建数据库 3、选中数据库 4、删除数据库 5、表操作 5.1查看数据库中的表 ​编辑 5.2创建表 5.2.1数据类型 5.3 查看指定表的表结构 5.4删除表 5.5 MySQL表的增删改查 5.5.1新增 / 插入数据…