當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringBoot2 整合 CXF 服务端和客户端
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot2 整合 CXF 服务端和客户端
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一、CXF服務(wù)端
- 1. 導(dǎo)入依賴
- 2. 創(chuàng)建service接口
- 3. 接口實(shí)現(xiàn)類
- 4. cxf配置類
- 5. 查看wsdl結(jié)果
- 二、CXF客戶端
- 2.1. 客戶端
- 2.2. 斷點(diǎn)調(diào)試
- 2.3. 發(fā)起調(diào)用服務(wù)
- 開(kāi)源源碼.
一、CXF服務(wù)端
1. 導(dǎo)入依賴
<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. 創(chuàng)建service接口
| @WebService | 放在接口上用于標(biāo)記為webService服務(wù)接口 |
| targetNamespace | 命名空間 |
| name | 服務(wù)接口的名字,可不寫 |
| @WebMethod | 標(biāo)記為webService服務(wù)的方法 |
| @WebParam | 標(biāo)記為webService服務(wù)的方法入?yún)?/td> |
3. 接口實(shí)現(xiàn)類
package com.gblfy.ws.service.impl;import com.gblfy.ws.service.ICxfService; import org.springframework.stereotype.Component;import javax.jws.WebService;/*** cxf接口實(shí)現(xiàn)類** @author gblfy* @date 2021-09-17*/ @WebService(serviceName = "cxfServiceShell",//對(duì)外發(fā)布的服務(wù)名targetNamespace = "http://service.ws.gblfy.com/",//指定你想要的名稱空間,通常使用使用包名反轉(zhuǎn)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();}/*** 發(fā)布服務(wù)并指定訪問(wèn)URL** @return*/@Beanpublic EndpointImpl userEnpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), cxfService());endpoint.publish("/cxfServiceShell");return endpoint;} }5. 查看wsdl結(jié)果
(1)配置啟動(dòng)端口 server.port: 8080
(2)啟動(dòng)springBoot啟動(dòng)類 輸入 localhost:8080/cxf 可以看到自己發(fā)布的服務(wù)
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客戶端調(diào)用(企業(yè)內(nèi)部已封裝)** @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";//調(diào)用服務(wù)CxfClient.cxfClientSingleParam(cxfUrl, method, reqXml);}/*** 單/多參調(diào)用工具類(Object類型)** @param cxfUrl url地址* @param method 調(diào)用方法名* @param reqXml 發(fā)送報(bào)文體* @return res 返回結(jié)果* @throws Exception 若有異常,在控制臺(tái)輸出異常,并將異常拋出*/public static String cxfClientSingleParam(String cxfUrl, String method, Object... reqXml) throws Exception {String res = null;// 創(chuàng)建動(dòng)態(tài)客戶端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("方法名",參數(shù)1,參數(shù)2,參數(shù)3....);objects = client.invoke(method, reqXml);res = objects[0].toString();System.out.println("返回?cái)?shù)據(jù):" + res);} catch (java.lang.Exception e) {e.printStackTrace();throw e;}return res;} }2.2. 斷點(diǎn)調(diào)試
2.3. 發(fā)起調(diào)用服務(wù)
開(kāi)源源碼.
https://gitee.com/gb_90/unified-access-center
總結(jié)
以上是生活随笔為你收集整理的SpringBoot2 整合 CXF 服务端和客户端的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 移动端小程序 腾讯地图sdk 当前位置
- 下一篇: gradle idea java ssm