如何利用SpringBoot搭建WebService服務接口
前言
在項目開發(fā)過程中經(jīng)常會碰到對接醫(yī)療軟件系統(tǒng)的時候對方要求提供WebService形式的接口,本篇文章記載了個人對接項目過程中整合并搭建的WebService形式的接口,希望對您能夠有所幫助!
一、在pom.xml文件中導入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> <!-- 這個主要是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接口實現(xiàn)類
1.RequestDTO通用入?yún)嶓w類
代碼如下(示例):
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; /** * 機構編碼 */ String orgCode; /** * 業(yè)務方法 */ String type; /** * 業(yè)務參數(shù),格式為JSON */ String msgData; /** * 登錄憑證 */ String loginToken; }
2.impl接口實現(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 { /** * 門診排隊叫號通用函數(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)嶓w類RequestDTO中的一致即可
3.service業(yè)務類中引入實現(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 { /** * 門診排隊叫號通用函數(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è)務 * @param requestDTO 通用入?yún)? * @param type 1:智慧病房,2:門診排隊叫號 * @return String */ public String currencyService(RequestDTO requestDTO,int type){ ........根據(jù)項目需求填寫實際業(yè)務代碼 return null; } } }
三、其他配置
1.application.yml
代碼如下(示例):
cxf: path: /qcs_ods
2.啟動類配置注解
代碼如下(示例):
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 核心架構是以BUS為核心,整合其他組件。 * Bus是CXF的主干, 為共享資源提供一個可配置的場所,作用類似于Spring的ApplicationContext,這些共享資源包括 * WSDl管理器、綁定工廠等。通過對BUS進行擴展,可以方便地容納自己的資源,或者替換現(xiàn)有的資源。默認Bus實現(xiàn)基于Spring架構, * 通過依賴注入,在運行時將組件串聯(lián)起來。BusFactory負責Bus的創(chuàng)建。默認的BusFactory是SpringBusFactory,對應于默認 * 的Bus實現(xiàn)。在構造過程中,SpringBusFactory會搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。 * 根據(jù)這些配置文件構建一個ApplicationContext。開發(fā)者也可以提供自己的配置文件來定制Bus。 */ @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } /** * 此方法作用是改變項目中服務名的前綴名,此處127.0.0.1或者localhost不能訪問時,請使用ipconfig查看本機ip來訪問 * 此方法被注釋后, 即不改變前綴名(默認是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/列出服務列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看實際的服務 * 新建Servlet記得需要在啟動類添加注解:@ServletComponentScan * * 如果啟動時出現(xiàn)錯誤:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet * 可能是springboot與cfx版本不兼容。 * 同時在spring boot2.0.6之后的版本與xcf集成,不需要在定義以下方法,直接在application.properties配置文件中添加: * cxf.path=/webservice(默認是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.最后瀏覽器訪問項目地址:http://localhost:9803/qcs_ods/services/qcsOdsService?wsdl提示:項目訪問地址的構成在代碼中都有標注,請仔細核對并根據(jù)實際需求進行修改;
2.訪問地址成功
總結
到此這篇關于如何利用SpringBoot搭建WebService服務接口的文章就介紹到這了,更多相關SpringBoot搭建WebService服務接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java.sql.SQLException問題解決以及注意事項
這篇文章主要給大家介紹了關于java.sql.SQLException問題解決以及注意事項的相關資料,這個問題其實很好解決,文中通過圖文將解決的辦法介紹的很詳細,需要的朋友可以參考下2023-07-07Java用jxl讀取excel并保存到數(shù)據(jù)庫的方法
這篇文章主要為大家詳細介紹了Java用jxl讀取excel并保存到數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Spring-AOP @AspectJ進階之如何綁定代理對象
這篇文章主要介紹了Spring-AOP @AspectJ進階之如何綁定代理對象的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot項目打包成jar后獲取classpath下文件失敗的解決
這篇文章主要介紹了SpringBoot項目打包成jar后獲取classpath下文件失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07