當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot2 整合 CXF 服务端和客户端
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot2 整合 CXF 服务端和客户端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、CXF服務端
- 1. 導入依賴
- 2. 創建service接口
- 3. 接口實現類
- 4. cxf配置類
- 5. 查看wsdl結果
- 二、CXF客戶端
- 2.1. 客戶端
- 2.2. 斷點調試
- 2.3. 發起調用服務
- 開源源碼.
一、CXF服務端
1. 導入依賴
<properties><cxf.version>3.3.1</cxf.version></properties><!-- CXF webservice --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.2.4.Final</version></dependency>2. 創建service接口
| @WebService | 放在接口上用于標記為webService服務接口 |
| targetNamespace | 命名空間 |
| name | 服務接口的名字,可不寫 |
| @WebMethod | 標記為webService服務的方法 |
| @WebParam | 標記為webService服務的方法入參 |
3. 接口實現類
package com.gblfy.ws.service.impl;import com.gblfy.ws.service.ICxfService; import org.springframework.stereotype.Component;import javax.jws.WebService;/*** cxf接口實現類** @author gblfy* @date 2021-09-17*/ @WebService(serviceName = "cxfServiceShell",//對外發布的服務名targetNamespace = "http://service.ws.gblfy.com/",//指定你想要的名稱空間,通常使用使用包名反轉endpointInterface = "com.gblfy.ws.service.ICxfService") @Component public class CxfServiceImpl implements ICxfService {@Overridepublic String sayhello(String request) {return "gblfy.com " + request;} }4. cxf配置類
package com.gblfy.ws.config;import com.gblfy.ws.service.ICxfService; import com.gblfy.ws.service.impl.CxfServiceImpl; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class CxfConfig {@Beanpublic ServletRegistrationBean cxfServlet() {return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic ICxfService cxfService() {return new CxfServiceImpl();}/*** 發布服務并指定訪問URL** @return*/@Beanpublic EndpointImpl userEnpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), cxfService());endpoint.publish("/cxfServiceShell");return endpoint;} }5. 查看wsdl結果
(1)配置啟動端口 server.port: 8080
(2)啟動springBoot啟動類 輸入 localhost:8080/cxf 可以看到自己發布的服務
http://localhost:8080/cxf/cxfServiceShell?wsdl
二、CXF客戶端
2.1. 客戶端
package com.gblfy.ws.client;import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.springframework.stereotype.Component;/*** cxf客戶端調用(企業內部已封裝)** @author gblfy* @date 2021-09-17*/ @Component public class CxfClient {public static void main(String[] args) throws Exception {String cxfUrl = "http://127.0.0.1:8080/cxf/cxfServiceShell?wsdl";String method = "sayhello";String reqXml = "1";//調用服務CxfClient.cxfClientSingleParam(cxfUrl, method, reqXml);}/*** 單/多參調用工具類(Object類型)** @param cxfUrl url地址* @param method 調用方法名* @param reqXml 發送報文體* @return res 返回結果* @throws Exception 若有異常,在控制臺輸出異常,并將異常拋出*/public static String cxfClientSingleParam(String cxfUrl, String method, Object... reqXml) throws Exception {String res = null;// 創建動態客戶端JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient(cxfUrl);// 需要密碼的情況需要加上用戶名和密碼// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));Object[] objects = new Object[0];try {// 基本格式:invoke("方法名",參數1,參數2,參數3....);objects = client.invoke(method, reqXml);res = objects[0].toString();System.out.println("返回數據:" + res);} catch (java.lang.Exception e) {e.printStackTrace();throw e;}return res;} }2.2. 斷點調試
2.3. 發起調用服務
開源源碼.
https://gitee.com/gb_90/unified-access-center
總結
以上是生活随笔為你收集整理的SpringBoot2 整合 CXF 服务端和客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 移动端小程序 腾讯地图sdk 当前位置
- 下一篇: (进阶篇)Redis6.2.0 集群 主