SpringBoot WebService服務(wù)端&客戶端使用案例教程
更新時間:2023年10月20日 10:21:28 作者:知識淺談
這篇文章主要介紹了SpringBoot WebService服務(wù)端&客戶端使用案例教程,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
服務(wù)端:
依賴
<!-- webservice相關(guān)依賴--> <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.2.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.0</version> </dependency>
服務(wù)接口
package com.example.demo.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; /** * @author: Yinlei * Package: com.example.demo.service * @date: 2023-10-18 8:40 * @Description: webservice測試 * @version: 1.0 */ @WebService(name = "HelloWebService", targetNamespace = "http://helloWebService.service.demo.example.com") public interface HelloWebService { @WebMethod @WebResult(name = "resultName") String get(@WebParam(name = "name") String name); }
服務(wù)接口實現(xiàn)類
package com.example.demo.service.Impl; import com.example.demo.service.HelloWebService; import org.springframework.stereotype.Service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; /** * @author: Yinlei * Package: com.example.demo.service.Impl * @date: 2023-10-18 8:46 * @Description: * @version: 1.0 */ @Service @WebService(name = "HelloWebService", targetNamespace = "http://helloWebService.service.demo.example.com", //命名空間,一般是對應(yīng)的路徑反過來 endpointInterface = "com.example.demo.service.HelloWebService") //實現(xiàn)接口的地址 public class HelloWebServiceImpl implements HelloWebService { @Override public String get( String name) { return name; } }
cxf配置類
package com.example.demo.config; import com.example.demo.service.HelloWebService; 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.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; /** * @author: Yinlei * Package: com.example.demo.config * @date: 2023-10-18 10:09 * @Description: Cxf配置類 * @version: 1.0 */ @Configuration public class CxfConfig { @Autowired private HelloWebService helloWebService; @Bean public ServletRegistrationBean disServlet(){ return new ServletRegistrationBean(new CXFServlet(),"/webService/*"); //webservice下的請求將有CXFServlet處理 } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus(){ return new SpringBus(); } @Bean public Endpoint endpoint(){ EndpointImpl endpoint = new EndpointImpl(springBus(), helloWebService); endpoint.publish("/helloWebservice"); return endpoint; } }
客戶端
@GetMapping("/get1") public String get1() throws MalformedURLException { //創(chuàng)建WSDL文件的URL,這個參數(shù)為暴露webervice的網(wǎng)址 URL wsdlLocation = new URL("http://localhost:9000/webService/helloWebservice?wsdl"); //創(chuàng)建服務(wù)名稱:第一個參數(shù)為命名空間地址,第二個參數(shù) 為本地部分的命名 HelloWebServiceImplService 這個是對應(yīng)的實現(xiàn)類名加上Service QName serviceName = new QName("http://helloWebService.service.demo.example.com", "HelloWebServiceImplService"); Service service = Service.create(wsdlLocation, serviceName); //獲取服務(wù)實現(xiàn)類 參數(shù)為對應(yīng)的服務(wù)接口.class HelloWebService port = service.getPort(HelloWebService.class); //調(diào)用方法 String asd = port.get("asd"); return asd; }
到此這篇關(guān)于SpringBoot WebService服務(wù)端&客戶端使用教程的文章就介紹到這了,更多相關(guān)SpringBoot WebService服務(wù)端內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于JAVA中stream流的基礎(chǔ)處理(獲取對象字段和對象批量處理等)
這篇文章主要介紹了關(guān)于JAVA中stream流的基礎(chǔ)處理,包含獲取對象字段、按字段排序、按字段去重、對象批量處理、指定字段轉(zhuǎn)數(shù)組等內(nèi)容,需要的朋友可以參考下2023-03-03SpringCloud使用FFmpeg對視頻壓縮處理的代碼示例
在現(xiàn)代的視頻處理系統(tǒng)中,壓縮視頻以減小存儲空間、加快傳輸速度是一項非常重要的任務(wù),FFmpeg作為一個強大的開源工具,廣泛應(yīng)用于音視頻的處理,包括視頻的壓縮和格式轉(zhuǎn)換等,本文將通過Java代碼示例,向您展示如何使用FFmpeg進行視頻壓縮,并介紹相關(guān)參數(shù)的設(shè)置2024-11-11Java設(shè)計模式之備忘錄模式實現(xiàn)對象狀態(tài)的保存和恢復(fù)
本文介紹Java設(shè)計模式之備忘錄模式,該模式可以實現(xiàn)對象狀態(tài)的保存和恢復(fù)。通過詳細講解備忘錄模式的原理、實現(xiàn)方法和應(yīng)用場景,幫助讀者深入理解該設(shè)計模式,并提供示例代碼和技巧,便于讀者實際應(yīng)用2023-04-04SpringCloud中的Feign服務(wù)間的調(diào)用詳解
這篇文章主要介紹了SpringCloud中的Feign服務(wù)間的調(diào)用詳解,Feign 是一個聲明式的 REST 客戶端,它能讓 REST 調(diào)用更加簡單,Feign 供了 HTTP 請求的模板,通過編寫簡單的接口和插入注解,就可以定義好 HTTP 請求的參數(shù)、格式、地址等信息,需要的朋友可以參考下2024-01-01