通過springboot發(fā)布WebService接口并調(diào)用方式
SpringBoot集成cxf步驟記錄
1.什么是cxf
Apache CXF是一個開源的Service框架,簡化用戶的service開發(fā),基于CXF開發(fā)的應用可提供SOAP、XML/HTTP、RESTFUL HTTP或CORBA等服務。CXF底層頁可以使用不同的傳輸協(xié)議,包括HTTP、JMS或JBI等。
cxf特性
支持大量的Web Service標準:包括SOAP、WS-I Basic Profile、WSDL、WS-Addressing、WS-Policy、WS-ReliableMessaging和WS-Security。
支持大量的前端(frontend)編程模型。CXF實現(xiàn)了標準的JAX-WS API,它也包括一種被稱為簡單前端(simple frontend)的模型,這種模型無需annotation支持。
CXF支持web service的兩種開發(fā)模式:
- 規(guī)則(contract)優(yōu)先: 通過編寫WSDL來開發(fā)web service;
- 代碼優(yōu)先: 通過編寫java代碼來開發(fā)webservice.
2.集成引入
<!-- cxf start -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.3.0</version>
</dependency>
<!-- cxf end -->
注意事項:cxf版本需要與SpringBoot的版本對應,不然因版本問題, 整合cxf-spring-boot-starter-jaxws的啟動項目會出現(xiàn)異常。
具體對應關(guān)系上倉庫進行查看下
https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws查找到jaxws的各種版本,進入之后可以看到對應的springboot版本
3.服務端
3.1 CXF配置類
package com.mrxu.config;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.ext.logging.LoggingFeature;
import org.springframework.context.annotation.Bean;
/**
* <b>功能描述:CXF配置類</b><br>
*
* @author newzhong
* @version 1.0.0
* @since JDK 1.8
*/
public class CxfConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
SpringBus bus = new SpringBus();
bus.getFeatures().add(new LoggingFeature());
return bus;
}
}
3.2 服務的發(fā)布
自定義注解標注要發(fā)布的服務類,發(fā)布出去
package com.mrxu.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>description:自動發(fā)布接口地址注解</p>
*
* @author newzhong
* @version 1.0
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoPublish {
/**
*<p>description:發(fā)布地址</p>
* @return String
* @author newzhong
*/
String publishAddress();
}
package com.mrxu.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
@Component
@Slf4j
public class PublishEndpoint implements ApplicationRunner {
@Autowired
private WebApplicationContext applicationConnect;
@Autowired()
@Qualifier(Bus.DEFAULT_BUS_ID)
private SpringBus bus;
@SuppressWarnings("resource")
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
log.info("開始進行自動發(fā)布webService接口");
String[] beanNames = applicationConnect.getBeanNamesForAnnotation(AutoPublish.class);
for(String beanName : beanNames) {
String publishAddr = applicationConnect.getType(beanName).getAnnotation(AutoPublish.class).publishAddress();
EndpointImpl endpoint = new EndpointImpl(bus, applicationConnect.getBean(beanName));
endpoint.publish(publishAddr);
log.info(String.format("發(fā)布接口地址:[%s]", publishAddr));
}
log.info("weBservice接口自動發(fā)布結(jié)束");
}
}
3.3 發(fā)布的地址yml,nacos配置
cxf:
path: /cxf
原本默認的默認端口號后 +拼接 /service,現(xiàn)在可以根據(jù)需求進行修改
3.4. 新增服務接口
在接口上添加@WebService注解
- @WebParam表示方法的參數(shù),如果不加此注解,方法的參數(shù)都從arg0,開始,隨著參數(shù)增多,name不斷增加為arg1,arg2…;
- @WebResult表示方法的返回值, 沒有此注解 返回值名字為return
package com.mrxu.service;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* <b>功能描述:webService測試接口</b><br>
* @author newzhong
* @version 1.0.0
* @since JDK 1.8
*
* @Note
* <b>創(chuàng)建時間:</b> 2021-03-27 14:32
*/
@WebService(targetNamespace = "http://service.mrxu.com/")
public interface IWebServiceTest {
String getDept(@WebParam(name = "jsonStr")String jsonStr);
}
3.5 新增服務接口實現(xiàn)類
- serviceName: 對外發(fā)布的服務名,指定 Web Service 的服務名稱:wsdl:service。缺省值為 Java 類的簡單名稱 + Service。(字符串)
- endpointInterface: 服務接口全路徑, 指定做SEI(Service EndPoint Interface)服務端點接口
- name:此屬性的值包含XML Web Service的名稱。在默認情況下,該值是實現(xiàn)XML Web Service的類的名稱,wsdl:portType 的名稱。缺省值為 Java 類或接口的非限定名稱。(字符串
- portName: wsdl:portName。缺省值為 WebService.name+Port。
- targetNamespace:指定你想要的名稱空間,認是使用接口實現(xiàn)類的包名的反綴
- wsdlLocation:指定用于定義 Web Service 的 WSDL 文檔的 Web 地址。Web 地址可以是相對路徑或絕對路徑。(字符串)
注意:實現(xiàn)類上可以不添加Webservice注解 ,加在接口上
package com.mrxu.service.impl;
import com.mrxu.common.domain.Phone;
import com.mrxu.config.AutoPublish;
import com.mrxu.dao.PhoneMapper;
import com.mrxu.service.IWebServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
import java.util.List;
@Service("WebServiceTest")
@AutoPublish(publishAddress = "test")
@WebService(endpointInterface = "com.mrxu.service.IWebServiceTest",
serviceName = "WebServiceTest",
targetNamespace = "http://service.mrxu.com/"
)
public class WebServiceTest implements IWebServiceTest {
@Autowired
public PhoneMapper phoneMapper;
@Override
public String getDept(String jsonStr) {
List<Phone> phones = phoneMapper.selectPhone();
String s = phones.toString();
return s;
}
}
3.6 驗證服務發(fā)布
通過瀏覽器訪問wsdl,wsdl路徑即為發(fā)布的路徑加上?wsdl
http://127.0.0.1:[端口號]/cxf/test?wsdl

可以看到接口就成功了。
測試webservice接口
1.新建一個controller
package com.mrxu.controller;
import com.mrxu.service.TestWebService;
import io.swagger.annotations.Api;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "webser")
@RestController
@RequestMapping("/wbser")
public class WebServiceController {
private static final long CONNECT_TIMEOUT = 10 * 1000;
private static final long RECEIVE_TIMEOUT = 60 * 1000;
private static final String ACTINFO_WSDL = "http://175.2.70.225:9002/cxf/test?wsdl";
private TestWebService service = getProxyService(ACTINFO_WSDL, TestWebService.class);
@PostMapping("/wbser")
public String wbeser(String id){
String userService = service.getDept("1");
return userService;
}
public static <T> T getProxyService(String wsdl, Class<T> serviceClass) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(serviceClass);
factory.setAddress(wsdl);
T service = (T) factory.create();
org.apache.cxf.endpoint.Client proxy = ClientProxy.getClient(service);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(CONNECT_TIMEOUT);
policy.setReceiveTimeout(RECEIVE_TIMEOUT);
conduit.setClient(policy);
return service;
}
}
2.新建一個WebService接口
package com.mrxu.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(targetNamespace="http://service.mrxu.com/")
public interface TestWebService {
@WebMethod
@WebResult
String getData(@WebParam String requestData);
@WebMethod
@WebResult
String getData1(@WebParam String requestData);
@WebMethod
@WebResult
String getDept(@WebParam(name = "jsonStr")String id);
}
最后可以通過swagger進行測試
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring @Transactional 自調(diào)用問題深度解析及解決方案
這篇文章主要介紹了Spring @Transactional 自調(diào)用問題深度解析及解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-04-04
Mybatis查詢數(shù)據(jù)的項目實現(xiàn)
MyBatis通過XML配置文件或注解,把Java對象映射到對應的數(shù)據(jù)庫表中,實現(xiàn)對象關(guān)系和數(shù)據(jù)關(guān)系的互相轉(zhuǎn)換,從而使得Java應用程序能夠更簡單的操作和讀取數(shù)據(jù)庫,本文就詳細的介紹一下如何實現(xiàn),感興趣的可以了解一下2023-09-09
如何在 Spring Boot 中配置和使用 CSRF 保護
CSRF是一種網(wǎng)絡攻擊,它利用已認證用戶的身份來執(zhí)行未經(jīng)用戶同意的操作,Spring Boot 提供了內(nèi)置的 CSRF 保護機制,可以幫助您防止這種類型的攻擊,這篇文章主要介紹了Spring?Boot?中的?CSRF?保護配置的使用方法,需要的朋友可以參考下2023-09-09
Java實戰(zhàn)之校園外賣點餐系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java實現(xiàn)簡易的校園外賣點餐系統(tǒng),文中采用的技術(shù)有:JSP、Spring、SpringMVC、MyBatis 等,感興趣的可以了解一下2022-03-03
Java實現(xiàn)數(shù)組的復制及深淺拷貝的常見方法總結(jié)
這篇文章主要為大家詳細介紹了Java中數(shù)組的復制及深淺拷貝的常見實現(xiàn)方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2026-01-01
Java concurrency集合之ArrayBlockingQueue_動力節(jié)點Java學院整理
ArrayBlockingQueue是數(shù)組實現(xiàn)的線程安全的有界的阻塞隊列。下面通過本文給大家介紹Java concurrency集合之ArrayBlockingQueue的相關(guān)知識,感興趣的朋友一起看看吧2017-06-06
Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài)
這篇文章主要介紹了Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài),本文講解了 基本類型轉(zhuǎn)換、 upcast與多態(tài)、 Object類等內(nèi)容,需要的朋友可以參考下2014-09-09

