SpringBoot使用CXF集成WebService的方法
1、寫在前面
WebService 對(duì)我來(lái)說(shuō)既熟悉又陌生,已經(jīng)將近六七年沒有看到過(guò)他了, 具體的介紹我就不多少了, 想了解的百度百科下說(shuō)的很詳細(xì)。
之所以突然研究WebService是接到一個(gè)需求要去給 XX 項(xiàng)目做一個(gè)適配層,他們?cè)邢到y(tǒng)是使用webservice做的,所以……
那我們就來(lái)看看,這一個(gè)古老的技術(shù)如何和如今最流行的框架SpringBoot進(jìn)行結(jié)合。
2、SpringBoot 集成WebService
2.1 導(dǎo)入依賴
compile('org.springframework.boot:spring-boot-starter-web-services',
'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.5',
'org.apache.cxf:cxf-rt-frontend-jaxws:3.1.6',
'org.apache.cxf:cxf-rt-transports-http:3.1.6')
我是用Gradle 來(lái)構(gòu)建項(xiàng)目的,使用Maven一樣,添加以上jar依賴就可以了。
2.2 開發(fā)Webservice接口
/**
* serviceName 服務(wù)名稱
* targetNamesPace : 一般都是接口包倒序,也可以自定義
@javax.jws.WebService(serviceName = "gzcxfw_wx_webserviceService",
targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx")
public interface WebService {
/**
*
* @param wxid 微信ID
* @param xm 姓名
* @param sfzh 身份證號(hào)
* @param sjh 手機(jī)號(hào)
* @param macId 預(yù)定用戶
* @param password 密碼
* @return 查詢結(jié)果 Base64字符串
*/
@WebMethod(operationName = "gzcxfw_hlw_wxrz_Info_cs")
@WebResult(name = "gzcxfw_hlw_wxrz_Info_csReturn")
String gzcxfwHlwWxrzInfoCs(@WebParam(name = "WXID", targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String wxid, @WebParam(name = "XM") String xm,
@WebParam(name = "SFZH", targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String sfzh, @WebParam(name = "SJH") String sjh,
@WebParam(name = "mac_id", targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String macId, @WebParam(name = "password") String password
);
2.3 實(shí)現(xiàn)類
/**
* @author yueli
* @date 2019-08-05 19:17
*/
@javax.jws.WebService(serviceName = "gzcxfw_wx_webserviceService",
targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx")
public class WebServiceImpl implements WebService {
private static Logger logger = LoggerFactory.getLogger(CatalogInfoImpl.class);
@Override
public String gzcxfwHlwWxrzInfoCs(String wxid, String xm, String sfzh, String sjh, String macId, String password) {
logger.info("gzcxfwHlwWxrzInfoCs: 入?yún)? wxid:{}, xm:{}, sfzh:{}, sjh:{}, macId:{}, pawd:{}", wxid, xm, sfzh,
macId, password);
return wxid + “:” + sfzh;
}
實(shí)現(xiàn)類就很簡(jiǎn)單了。和我們一般寫的沒啥區(qū)別,
2.4 發(fā)布
package com.tusdao.base.configuration;
import com.tusdao.TusdaoWebserviceApplication;
import com.tusdao.webservice.service.WebService;
import com.tusdao.webservice.service.impl.WebServiceImpl;
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.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* @author yueli
* @date 2019-08-05 19:24
*/
@Configuration
@ComponentScan(basePackageClasses = TusdaoWebserviceApplication.class)
public class CxfConfig {
@SuppressWarnings("all")
@Bean(name = "cxfServletRegistration")
public ServletRegistrationBean dispatcherServlet() {
//創(chuàng)建服務(wù)并指定服務(wù)名稱
return new ServletRegistrationBean(new CXFServlet(), "/axis/services/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public WebService webService() {
return new WebServiceImpl();
}
/**
* 注冊(cè)WebServiceDemoService接口到webservice服務(wù)
*
* @return
*/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), webService());
endpoint.publish("/bdcgzcxfw_wx");
endpoint.getInInterceptors().add(new ServerNameSpaceInterceptor());
//endpoint.getInInterceptors().add(new InInterceptor());
return endpoint;
}
}
這個(gè)就簡(jiǎn)單了, 我們?cè)谑褂脮r(shí)可以直接copy過(guò)去就行。
最后就是啟動(dòng)項(xiàng)目了。
啟動(dòng)后我們直接輸入項(xiàng)目地址:http://localhost:8090/指定的服務(wù)名

會(huì)看到生成的ssdl。到這基本搭建就結(jié)束了。測(cè)試在這就不寫了, 大家可以使用wsdl生成客戶端,或者直接使用http發(fā)送xml格式數(shù)據(jù)進(jìn)行請(qǐng)求。
3、總結(jié)
springboot使用CXF集成Webservice 開發(fā)很簡(jiǎn)單,不用在單獨(dú)的部署到外部tomcat上, 這為我們熟悉springboot開發(fā)的同學(xué)帶了很好的體驗(yàn)。
有想要完整實(shí)例的請(qǐng)看著 >> https://github.com/yuelicn/springboot-webservice-cxf
或者直接clone >> https://github.com/yuelicn/springboot-webservice-cxf
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot中配置Mail和普通mail的實(shí)現(xiàn)方式
這篇文章主要介紹了Springboot中配置Mail和普通mail的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
SpringBoot中HTTP請(qǐng)求不通的原因級(jí)解決方法
HTTP 請(qǐng)求是指從客戶端到服務(wù)器的請(qǐng)求消息,對(duì)于一個(gè) Spring Boot 項(xiàng)目而言,服務(wù)器就是 Spring Boot,客戶端就是用戶本地的瀏覽器,但是會(huì)遇到SpringBoot HTTP請(qǐng)求不通的問(wèn)題,本文介紹了一些常見問(wèn)題及解決方法,需要的朋友可以參考下2025-02-02
Java中的HashSet詳解和使用示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
HashSet 是一個(gè)沒有重復(fù)元素的集合。接下來(lái)通過(guò)實(shí)例代碼給大家介紹java中的hashset相關(guān)知識(shí),感興趣的朋友一起看看吧2017-05-05
SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式
這篇文章主要介紹了SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

