當前位置:
首頁 >
SpringBoot——实现WebService接口服务端以及客户端开发
發布時間:2025/3/12
26
豆豆
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot——实现WebService接口服务端以及客户端开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、服務端代碼開發
- 1、pom依賴
- 2、接口類
- 3、接口實現類
- 4、webservice配置文件
- 2、客戶端開發
- (1)pom依賴
- (2)封裝客戶端方法clientUtil
- (3)調用接口類
- (4)運行結果
我們經常需要在兩個系統之間進行一些數據的交互,這時候我們就需要開發數據交互接口。
一般來說,遇到比較多的接口有HTTP接口、WebService接口、FTP文件傳輸。今天我要來學習一下在SpringBoot框架下進行簡單的webservice接口的開發。
一、服務端代碼開發
創建了兩個wbservice接口TestService和CatService。
1、pom依賴
導入相關的依賴包。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.6</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.6</version></dependency>2、接口類
import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.ws.WebServiceProvider;@WebService(name = "TestService", // 暴露服務名稱targetNamespace = "http://server.webservice.Bag.admin.com"// 命名空間,一般是接口的包名倒序 ) public interface TestService {@WebMethodpublic String sendMessage(@WebParam(name = "username") String username);@WebMethodpublic boolean getFlag(@WebParam(name = "username") String username); } import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService;@WebService(name = "CatService", // 暴露服務名稱targetNamespace = "http://server.webservice.Bag.admin.com"// 命名空間,一般是接口的包名倒序 ) public interface CatService {@WebMethodpublic String message(@WebParam(name = "name") String name); }3、接口實現類
import com.admin.Bag.webservice.server.TestService; import org.springframework.stereotype.Component;import javax.jws.WebService;@WebService(serviceName = "TestService", // 與接口中指定的name一致targetNamespace = "http://server.webservice.Bag.admin.com", // 與接口中的命名空間一致,一般是接口的包名倒endpointInterface = "com.admin.Bag.webservice.server.TestService"// 接口地址 ) @Component public class TestServiceImpl implements TestService {@Overridepublic String sendMessage(String username) {return "=====Hello! " + username + "=====";}@Overridepublic boolean getFlag(String username) {//return true;} } import com.admin.Bag.webservice.server.CatService; import org.springframework.stereotype.Component;import javax.jws.WebService;@WebService(serviceName = "CatService", // 與接口中指定的name一致targetNamespace = "http://server.webservice.Bag.admin.com", // 與接口中的命名空間一致,一般是接口的包名倒endpointInterface = "com.admin.Bag.webservice.server.CatService"// 接口地址 ) @Component public class CatServiceImpl implements CatService {@Overridepublic String message(String name) {//return "一只小貓貓";} }4、webservice配置文件
import com.admin.Bag.webservice.server.impl.CatServiceImpl; import com.admin.Bag.webservice.server.impl.TestServiceImpl; 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;import javax.xml.ws.Endpoint;@Configuration public class cxfConfig {@Beanpublic ServletRegistrationBean disServlet() {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webService/*");return servletRegistrationBean;}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), new TestServiceImpl());endpoint.publish("/TestService");return endpoint;}@Beanpublic Endpoint endpoint2() {EndpointImpl endpoint = new EndpointImpl(springBus(), new CatServiceImpl());endpoint.publish("/CatService");return endpoint;}}啟動項目。我的項目端口號是8080。瀏覽器訪問地址:http://localhost:8082/webService
可見接口信息CatService和TestService,點進鏈接可以看每個接口的wsdl文檔。
2、客戶端開發
客戶端是一個單獨的項目。
(1)pom依賴
不同的SpringBoot版本對應的依賴版本也不一樣,我也是試了好久終于成了。我的SpringBoot版本號是2.3.0.RELEASE。
<!-- 進行jaxes 服務開發 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><!-- 內置jetty web服務器 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version></dependency>(2)封裝客戶端方法clientUtil
import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;public class clientUtil {public static String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient(wsdUrl);//client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));Object[] objects;// invoke("方法名",參數1,參數2,參數3....);objects = client.invoke(operationName, params);return objects[0].toString();} }(3)調用接口類
使用定時調用webservice接口。
import com.admin.webAppoint.webservice.client.clientUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RestController;@RestController @Component public class TestController {//在一個方法中連續調用多次WebService接口,每次調用前需要重置上下文。ClassLoader classLoader = Thread.currentThread().getContextClassLoader();@Scheduled(cron="*/15 * * * * ?")public String getMessage() {Thread.currentThread().setContextClassLoader(classLoader);//在獲取連接之前 還原上下文System.out.println("======開始調用webservice接口=====");String url = "http://localhost:8082/webService/CatService?wsdl";String methodName = "message";System.out.println("Calling" + url);String result="";try {result=clientUtil.callWebSV(url, methodName, "name");} catch (Exception e) {System.err.println("接口調用失敗!!!!");return "失敗";}System.out.println("===Finished!===恭喜你啊!!!CatService接口調用成功!!!===獲得的數據是:"+result);return "Finished!";}@Scheduled(cron="*/5 * * * * ?")public String getMessage2() {Thread.currentThread().setContextClassLoader(classLoader);//在獲取連接之前 還原上下文System.out.println("======開始調用webservice接口=====");String url = "http://localhost:8082/webService/TestService?wsdl";String methodName = "sendMessage";System.out.println("Calling" + url);String result="";try {result=clientUtil.callWebSV(url, methodName, "username");} catch (Exception e) {System.err.println("接口調用失敗!!!!");return "失敗";}System.out.println("===Finished!===恭喜你啊!!!TestService接口調用成功!!!===獲得的數據是:"+result);return "Finished!";} }(4)運行結果
首先啟動服務端。啟動客戶端。
遇到過報錯:
報錯——使用cxf時報錯:org.apache.cxf.interceptor.Fault: Marshalling Error: XXX is not known to this context
最終成功調用服務端的webservice接口:
總結
以上是生活随笔為你收集整理的SpringBoot——实现WebService接口服务端以及客户端开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vb.net2019-多线程并行计算(3
- 下一篇: 使用jackson对Java对象与JSO