SpringBoot整合WebService的實戰(zhàn)案例
WebService
Web服務(Web Services)是一種基于網絡的標準化的軟件系統(tǒng),允許不同的應用程序通過網絡相互通信和交互。它們使用標準化的網絡協(xié)議和數據格式,使得不同平臺、不同語言編寫的應用程序能夠互相通信和交換數據。
在現代軟件開發(fā)中,構建可靠的Web服務是至關重要的。Apache CXF是一個功能強大的Java框架,用于構建Web服務和Web應用程序。結合Spring Boot,我們可以快速搭建一個簡單的WebService。本文將介紹如何使用Apache CXF和Spring Boot創(chuàng)建一個簡單的WebService,并進行基本的測試。
1.簡單介紹WebService
1.1. 類型
Web服務通常分為兩種主要類型:
- SOAP Web服務:基于SOAP(Simple Object Access Protocol)協(xié)議的Web服務。SOAP是一種用于交換結構化信息的協(xié)議,它使用XML作為消息格式,并通常通過HTTP協(xié)議進行傳輸。
- RESTful Web服務:基于REST(Representational State Transfer)原則的Web服務。RESTful服務使用標準的HTTP方法(如GET、POST、PUT、DELETE)來執(zhí)行操作,并通常返回JSON或XML格式的數據。
1.2. 架構
Web服務的架構通常包括以下關鍵組件:
- 服務提供者(Service Provider):提供Web服務的實體。它們發(fā)布服務并處理來自客戶端的請求。
- 服務請求者(Service Requestor):使用Web服務的客戶端應用程序。它們向服務提供者發(fā)送請求并處理響應。
- 服務描述(Service Description):Web服務的描述文件,通常使用WSDL(Web Services Description Language)或OpenAPI等格式來描述服務的接口和操作。
- 消息格式(Message Format):Web服務使用的數據交換格式,通常是XML或JSON。
- 通信協(xié)議(Communication Protocol):Web服務之間通信的協(xié)議,常見的包括HTTP、HTTPS、SMTP等。
1.3. 主要特點
Web服務具有以下主要特點:
- 跨平臺性(Platform Independence):由于Web服務使用標準化的協(xié)議和數據格式,因此它們可以在不同的平臺和操作系統(tǒng)上運行。
- 松耦合(Loose Coupling):Web服務通過標準化接口進行通信,服務提供者和請求者之間的耦合度較低,可以獨立開發(fā)和部署。
- 可組合性(Composability):可以通過組合多個Web服務來創(chuàng)建復雜的應用程序。
- 可重用性(Reusability):Web服務可以被多個應用程序重復使用,從而提高了軟件開發(fā)效率。
- 易于維護(Maintainability):由于Web服務使用標準化的接口和協(xié)議,因此易于維護和更新。
1.4. 使用場景
Web服務在許多場景下都得到了廣泛應用,包括但不限于:
- 企業(yè)應用集成(Enterprise Application Integration,EAI):將不同的企業(yè)應用程序和系統(tǒng)集成在一起,實現數據和業(yè)務流程的無縫交互。
- 分布式系統(tǒng):構建分布式系統(tǒng)和服務導向架構(Service-Oriented Architecture,SOA),提供跨網絡的服務和資源共享。
- 移動應用程序開發(fā):通過Web服務為移動應用程序提供數據和功能支持,與后端服務器進行通信和交互。
- 云計算:在云平臺上部署和管理Web服務,提供云端服務和資源。
1.5. Web服務標準和技術
一些常見的Web服務標準和技術包括:
- SOAP(Simple Object Access Protocol):用于構建基于XML的Web服務的協(xié)議。
- WSDL(Web Services Description Language):用于描述Web服務的接口和操作的XML格式的語言。
- UDDI(Universal Description, Discovery, and Integration):用于注冊和發(fā)現Web服務的協(xié)議和規(guī)范。
- REST(Representational State Transfer):一種基于HTTP協(xié)議的軟件架構風格,用于構建RESTful Web服務。
- JSON(JavaScript Object Notation):一種輕量級的數據交換格式,通常用于RESTful Web服務的數據格式。
2.案例-WebServiceDemo
2.1.引入配置文件
首先,我們需要在項目中添加必要的依賴項。這些依賴項將幫助我們集成Apache CXF到Spring Boot應用程序中。我的使用的是gradle構建的項目
// 引入WebService
implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.2.0'
implementation 'org.apache.cxf:cxf-rt-transports-http:3.2.0'
<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>2.2.創(chuàng)建接口
import com.fhr.student.entity.Student;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface StudentService {
/**
* 根據姓名獲取學生信息
* @param userName 學生姓名
* @return 學生信息
*/
Student getStudentInfoByName(@WebParam(name = "userName")String userName);
}2.3.創(chuàng)建接口實現類
import com.fhr.service.StudentService;
import com.fhr.student.entity.Student;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
/**
* targetNamespace:目標命名控件,一般由接口所在包路徑命名,不過是由里往外寫:比如:我接口所在路徑為:com.fhr.service 寫為:http://service.fhr.com/
*/
@Component
@WebService(targetNamespace = "http://service.fhr.com/",endpointInterface = "com.fhr.service.StudentService")
public class StudentImpl implements StudentService {
/**
* 根據學生姓名獲取學生信息
* @param userName 學生姓名
* @return 學生信息
*/
@Override
public Student getStudentInfoByName(String userName) {
// TODO這里應該查詢數據庫
System.out.println("傳入的參數為:"+userName);
Student student = new Student();
student.setUserName(userName);
student.setClassName("高三一班");
student.setAge(14);
return student;
}
}
2.4.創(chuàng)建WebService配置類
我們需要配置CXF和發(fā)布WebService的端點。我們使用Spring Boot的配置類來完成這個任務。
import com.fhr.service.StudentService;
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 WebServiceConfig {
// 創(chuàng)建一個SpringBus Bean,作為Apache CXF的默認總線
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
// 注冊CXF Servlet,用于處理WebService請求
@Bean(name = "wbsBean")
public ServletRegistrationBean dispatcherServlet() {
// 創(chuàng)建一個ServletRegistrationBean,將CXFServlet映射到指定路徑
ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/wbs/*");
return wbsServlet;
}
// 定義WebService端點
@Bean
public Endpoint endpointPurchase(SpringBus springBus, StudentService studentService) {
// 創(chuàng)建EndpointImpl對象,并將SpringBus和WebService實現類傳入
EndpointImpl endpoint = new EndpointImpl(springBus, studentService);
// 將端點發(fā)布到指定路徑
endpoint.publish("/user-server");
// 打印發(fā)布成功消息,顯示服務的訪問地址
System.out.println("服務發(fā)布成功!地址為:http://localhost:8081/wbs/user-server");
// 返回端點對象
return endpoint;
}
}2.5.測試
啟動項目后,您可以在瀏覽器中輸入http://localhost:8081/wbs/user-server?wsdl來查看WebService的WSDL文檔。
# 啟動項目 在瀏覽器的地址中輸入 http://localhost:8081/wbs/user-server?wsdl

# 測試客戶端 為了方便直接在本地項目測試 在瀏覽器中輸入 測試
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/stu")
public class StudentController {
// 定義了一個映射到路徑"/stu/getUserInfoByName"的GET請求處理方法
@GetMapping("/getUserInfoByName")
public Object[] getUserInfoByName(@RequestParam("name")String name){
// 創(chuàng)建JaxWsDynamicClientFactory實例,用于動態(tài)創(chuàng)建客戶端
JaxWsDynamicClientFactory proxyFactoryBean = JaxWsDynamicClientFactory.newInstance();
// 使用動態(tài)客戶端工廠創(chuàng)建客戶端對象,并指定WebService的WSDL地址
Client client = proxyFactoryBean.createClient("http://localhost:8081/wbs/user-server?wsdl");
// 定義一個Object數組用于存儲調用WebService方法后的返回結果
Object[] objects = new Object[0];
// 調用遠程WebService方法
try {
// 調用客戶端的invoke方法,傳入方法名和參數,獲取WebService方法的返回結果
objects = client.invoke("getStudentInfoByName", name);
} catch (Exception e) {
// 捕獲異常,打印異常信息
e.printStackTrace();
}
// 返回WebService方法的返回結果
return objects;
}
}
總結
到此這篇關于SpringBoot整合WebService的文章就介紹到這了,更多相關SpringBoot整合WebService內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis if test 不為空字符串且不為null的問題
這篇文章主要介紹了mybatis if test 不為空字符串且不為null的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
springboot+mongodb 實現按日期分組分頁查詢功能
這篇文章主要介紹了springboot+mongodb 實現按日期分組分頁查詢功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
MyBatis-Flex+ShardingSphere-JDBC多數據源分庫分表實現
本文介紹了使用MyBatis-Flex和ShardingSphere-JDBC實現多數據源分庫分表的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-10-10

