如何利用SpringBoot搭建WebService服務(wù)接口
前言
在項(xiàng)目開發(fā)過程中經(jīng)常會(huì)碰到對(duì)接醫(yī)療軟件系統(tǒng)的時(shí)候?qū)Ψ揭筇峁¦ebService形式的接口,本篇文章記載了個(gè)人對(duì)接項(xiàng)目過程中整合并搭建的WebService形式的接口,希望對(duì)您能夠有所幫助!
一、在pom.xml文件中導(dǎo)入WebService所需的Jar包
代碼如下:
<!--WebService--> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>stax2-api</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>woodstox-core-asl</artifactId> <version>4.4.1</version> </dependency> <!-- 這個(gè)主要是client訪問的,但是問題多多--> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>axis</groupId> <artifactId>axis-jaxrpc</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency> <!--WebService-->
二、定義WebService接口實(shí)現(xiàn)類
1.RequestDTO通用入?yún)?shí)體類
代碼如下(示例):
import lombok.Data; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * @author 孤巷. * @description * @date 2022/8/5 11:47 */ @XmlRootElement(name="ROOT") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder={"msgData", "loginToken","orgCode","type"}) @Data public class RequestDTO { private static final long serialVersionUID = 3428504463675931746L; /** * 機(jī)構(gòu)編碼 */ String orgCode; /** * 業(yè)務(wù)方法 */ String type; /** * 業(yè)務(wù)參數(shù),格式為JSON */ String msgData; /** * 登錄憑證 */ String loginToken; }
2.impl接口實(shí)現(xiàn)類
代碼如下(示例):
import com.example.web.dto.RequestDTO; import javax.jws.WebParam; import javax.jws.WebService; /** * @author 孤巷. * @description WebService函數(shù)定義 * @date 2022/8/9 10:30 */ @WebService(name = "qcsOdsImpl", targetNamespace = "http://server.webservice.example.com") public interface qcsOdsImpl { /** * 門診排隊(duì)叫號(hào)通用函數(shù) * @param requestDTO 通用入?yún)? * @return String-SM4加密密文 */ String publicInterfaceFun(@WebParam(name="ROOT") RequestDTO requestDTO); /** * 智慧病房通用函數(shù) * @param requestDTO 通用入?yún)? * @return String-SM4加密密文 */ String smartWard(@WebParam(name="ROOT") RequestDTO requestDTO); }
提示:其中的@WebParam(name="ROOT") 中的ROOT代表著方法的參數(shù),與通用入?yún)?shí)體類RequestDTO中的一致即可
3.service業(yè)務(wù)類中引入實(shí)現(xiàn)類
代碼如下(示例):
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.example.web.constant.Constant; import com.example.web.dto.RequestDTO; import com.example.web.model.MapMappingModel; import com.example.web.service.InitDataService; import com.example.web.util.MySM4Util; import com.example.web.util.ReflectUtil; import com.example.web.util.ResultUtil; import com.example.web.util.SpringContextUtils; import com.example.web.webservice.impl.qcsOdsImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.jws.WebService; import java.lang.reflect.InvocationTargetException; /** * @author 孤巷. * @description * @date 2022/8/9 10:30 */ @Component @WebService( targetNamespace = "http://server.webservice.example.com", endpointInterface = "com.example.web.webservice.impl.qcsOdsImpl") @Slf4j public class qcsOdsService implements qcsOdsImpl { /** * 門診排隊(duì)叫號(hào)通用函數(shù) * @param requestDTO 通用入?yún)? * @return String-SM4加密密文 */ @Override public String publicInterfaceFun(RequestDTO requestDTO) { return currencyService(requestDTO,1); } /** * 智慧病房通用函數(shù) * @param requestDTO 通用入?yún)? * @return String-SM4加密密文 */ @Override public String smartWard(RequestDTO requestDTO) { return currencyService(requestDTO,2); } /** * 通用業(yè)務(wù) * @param requestDTO 通用入?yún)? * @param type 1:智慧病房,2:門診排隊(duì)叫號(hào) * @return String */ public String currencyService(RequestDTO requestDTO,int type){ ........根據(jù)項(xiàng)目需求填寫實(shí)際業(yè)務(wù)代碼 return null; } } }
三、其他配置
1.application.yml
代碼如下(示例):
cxf: path: /qcs_ods
2.啟動(dòng)類配置注解
代碼如下(示例):
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) @EnableScheduling @MapperScan(value = {"com.example.web.dao.*"}) @ComponentScan(basePackages = "com.example.web.*") public class JavaWebserviceApplication { public static void main(String[] args) { SpringApplication.run(JavaWebserviceApplication.class, args); } }
2.WebServiceConfig配置
代碼如下(示例):
import javax.xml.ws.Endpoint; import com.example.web.webservice.qcsOdsService; 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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author 孤巷. * @description * @date 2022/8/4 14:04 */ @Configuration public class WebServiceConfig { @Autowired private qcsOdsService serverServiceDemo; /** * Apache CXF 核心架構(gòu)是以BUS為核心,整合其他組件。 * Bus是CXF的主干, 為共享資源提供一個(gè)可配置的場(chǎng)所,作用類似于Spring的ApplicationContext,這些共享資源包括 * WSDl管理器、綁定工廠等。通過對(duì)BUS進(jìn)行擴(kuò)展,可以方便地容納自己的資源,或者替換現(xiàn)有的資源。默認(rèn)Bus實(shí)現(xiàn)基于Spring架構(gòu), * 通過依賴注入,在運(yùn)行時(shí)將組件串聯(lián)起來。BusFactory負(fù)責(zé)Bus的創(chuàng)建。默認(rèn)的BusFactory是SpringBusFactory,對(duì)應(yīng)于默認(rèn) * 的Bus實(shí)現(xiàn)。在構(gòu)造過程中,SpringBusFactory會(huì)搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。 * 根據(jù)這些配置文件構(gòu)建一個(gè)ApplicationContext。開發(fā)者也可以提供自己的配置文件來定制Bus。 */ @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } /** * 此方法作用是改變項(xiàng)目中服務(wù)名的前綴名,此處127.0.0.1或者localhost不能訪問時(shí),請(qǐng)使用ipconfig查看本機(jī)ip來訪問 * 此方法被注釋后, 即不改變前綴名(默認(rèn)是services), wsdl訪問地址為 http://127.0.0.1:8080/services/ws/api?wsdl * 去掉注釋后wsdl訪問地址為:http://127.0.0.1:8080/soap/ws/api?wsdl * http://127.0.0.1:8080/soap/列出服務(wù)列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看實(shí)際的服務(wù) * 新建Servlet記得需要在啟動(dòng)類添加注解:@ServletComponentScan * * 如果啟動(dòng)時(shí)出現(xiàn)錯(cuò)誤:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet * 可能是springboot與cfx版本不兼容。 * 同時(shí)在spring boot2.0.6之后的版本與xcf集成,不需要在定義以下方法,直接在application.properties配置文件中添加: * cxf.path=/webservice(默認(rèn)是services) */ // @Bean // public ServletRegistrationBean dispatcherServlet() { // return new ServletRegistrationBean(new CXFServlet(), "/soap/*"); // } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), serverServiceDemo); endpoint.publish("/services/qcsOdsService"); return endpoint; } }
四、訪問地址
1.最后瀏覽器訪問項(xiàng)目地址:http://localhost:9803/qcs_ods/services/qcsOdsService?wsdl提示:項(xiàng)目訪問地址的構(gòu)成在代碼中都有標(biāo)注,請(qǐng)仔細(xì)核對(duì)并根據(jù)實(shí)際需求進(jìn)行修改;
2.訪問地址成功
總結(jié)
到此這篇關(guān)于如何利用SpringBoot搭建WebService服務(wù)接口的文章就介紹到這了,更多相關(guān)SpringBoot搭建WebService服務(wù)接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot調(diào)用webservice-soap接口的實(shí)現(xiàn)
- SpringBoot調(diào)用對(duì)方webService接口的幾種方法示例
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- webservice實(shí)現(xiàn)springboot項(xiàng)目間接口調(diào)用與對(duì)象傳遞示例
- SpringBoot項(xiàng)目使用?axis?調(diào)用webservice接口的實(shí)踐記錄
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
- springboot使用webservice發(fā)布和調(diào)用接口的實(shí)例詳解
相關(guān)文章
java.sql.SQLException問題解決以及注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于java.sql.SQLException問題解決以及注意事項(xiàng)的相關(guān)資料,這個(gè)問題其實(shí)很好解決,文中通過圖文將解決的辦法介紹的很詳細(xì),需要的朋友可以參考下2023-07-07一文帶你了解Java選擇排序的原理與實(shí)現(xiàn)
選擇排序:(Selection sort)是一種簡(jiǎn)單直觀的排序算法,也是一種不穩(wěn)定的排序方法。本文主要為大家介紹一下選擇排序的原理與實(shí)現(xiàn),希望對(duì)大家有所幫助2022-11-11java Unicode和UTF-8之間轉(zhuǎn)換實(shí)例
這篇文章主要介紹了java Unicode和UTF-8之間轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java用jxl讀取excel并保存到數(shù)據(jù)庫的方法
這篇文章主要為大家詳細(xì)介紹了Java用jxl讀取excel并保存到數(shù)據(jù)庫的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Spring-AOP @AspectJ進(jìn)階之如何綁定代理對(duì)象
這篇文章主要介紹了Spring-AOP @AspectJ進(jìn)階之如何綁定代理對(duì)象的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java中一個(gè)for語句導(dǎo)致無窮大死循環(huán)的例子
這篇文章主要介紹了Java中一個(gè)for語句導(dǎo)致無窮大死循環(huán)的例子,本文給出的是一個(gè)很特別的例子,這個(gè)例子會(huì)跟你所想的結(jié)果不一樣,需要的朋友可以參考下2015-06-06SpringBoot項(xiàng)目打包成jar后獲取classpath下文件失敗的解決
這篇文章主要介紹了SpringBoot項(xiàng)目打包成jar后獲取classpath下文件失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07