0


[webservice] springboot整合cxf

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.yening.gitee.com", name ="MedicalOrderSvc")publicclassMedicalOrderService{// 配置service中的method@WebMethod(operationName ="medicationOrderRequest",action="/medicationOrderRequest")@WebResult// 使用WebParam配置参数名称,不配置的话默认会是arg0之类的名称publicRmedicationOrderRequest(@WebParam(name ="message")String message){returnnull;}}

2.3 service发布(cxf配置)

@Configuration@EnableWspublicclassCxfConfig{/**
     * 注意:此方法被注释后WSDL访问地址是http://127.0.0.1:8080/services/helloService?wsdl
     * 放开注释后WSDL访问地址是:http://127.0.0.1:8080/ws/helloService?wsdl
     */@BeanpublicServletRegistrationBeanwsServletConfig(){returnnewServletRegistrationBean(newCXFServlet(),"/ws/*");}@ResourceprivateMedicalOrderService medicalOrderService;// 对于多个发布的service,可以写多个bean来实现@BeanpublicEndpointorderEndpoint(){returnformatEndpoint("/medicalOrderService", medicalOrderService);}/**
     * 格式化暴漏的ws接口信息
     *
     * @param path  暴露的ws请求地址
     * @param svc   与地址对应的ws服务
     * @return
     */@ResourceBus bus;privateEndpointformatEndpoint(String path,Object svc){EndpointImpl endpoint =newEndpointImpl(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("临时测试使用")@Testpublicvoidtest1(){MedicalOrderServiceService svc =newMedicalOrderServiceService();
        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第一个方法的描述")publicvoidtest2()throwsIOException,DocumentException{URL url =newURL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");URLConnection conn = url.openConnection();SAXReader saxReader =newSAXReader();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://blog.csdn.net/seven_zhao/article/details/134480603
版权归原作者 一饼团队 所有, 如有侵权,请联系我们删除。

“[webservice] springboot整合cxf”的评论:

还没有评论