文章目錄
- WebService
- 1.簡單介紹WebService
- 1.1. 類型
- 1.2. 架構
- 1.3. 主要特點
- 1.4. 使用場景
- 1.5. Web服務標準和技術
- 2.案例-WebServiceDemo
- 2.1.引入配置文件
- 2.2.創建接口
- 2.3.創建接口實現類
- 2.4.創建WebService配置類
- 2.5.測試
WebService
Web服務(Web Services)是一種基于網絡的標準化的軟件系統,允許不同的應用程序通過網絡相互通信和交互。它們使用標準化的網絡協議和數據格式,使得不同平臺、不同語言編寫的應用程序能夠互相通信和交換數據。
在現代軟件開發中,構建可靠的Web服務是至關重要的。Apache CXF是一個功能強大的Java框架,用于構建Web服務和Web應用程序。結合Spring Boot,我們可以快速搭建一個簡單的WebService。本文將介紹如何使用Apache CXF和Spring Boot創建一個簡單的WebService,并進行基本的測試。
1.簡單介紹WebService
1.1. 類型
Web服務通常分為兩種主要類型:
- SOAP Web服務:基于SOAP(Simple Object Access Protocol)協議的Web服務。SOAP是一種用于交換結構化信息的協議,它使用XML作為消息格式,并通常通過HTTP協議進行傳輸。
- RESTful Web服務:基于REST(Representational State Transfer)原則的Web服務。RESTful服務使用標準的HTTP方法(如GET、POST、PUT、DELETE)來執行操作,并通常返回JSON或XML格式的數據。
1.2. 架構
Web服務的架構通常包括以下關鍵組件:
- 服務提供者(Service Provider):提供Web服務的實體。它們發布服務并處理來自客戶端的請求。
- 服務請求者(Service Requestor):使用Web服務的客戶端應用程序。它們向服務提供者發送請求并處理響應。
- 服務描述(Service Description):Web服務的描述文件,通常使用WSDL(Web Services Description Language)或OpenAPI等格式來描述服務的接口和操作。
- 消息格式(Message Format):Web服務使用的數據交換格式,通常是XML或JSON。
- 通信協議(Communication Protocol):Web服務之間通信的協議,常見的包括HTTP、HTTPS、SMTP等。
1.3. 主要特點
Web服務具有以下主要特點:
- 跨平臺性(Platform Independence):由于Web服務使用標準化的協議和數據格式,因此它們可以在不同的平臺和操作系統上運行。
- 松耦合(Loose Coupling):Web服務通過標準化接口進行通信,服務提供者和請求者之間的耦合度較低,可以獨立開發和部署。
- 可組合性(Composability):可以通過組合多個Web服務來創建復雜的應用程序。
- 可重用性(Reusability):Web服務可以被多個應用程序重復使用,從而提高了軟件開發效率。
- 易于維護(Maintainability):由于Web服務使用標準化的接口和協議,因此易于維護和更新。
1.4. 使用場景
Web服務在許多場景下都得到了廣泛應用,包括但不限于:
- 企業應用集成(Enterprise Application Integration,EAI):將不同的企業應用程序和系統集成在一起,實現數據和業務流程的無縫交互。
- 分布式系統:構建分布式系統和服務導向架構(Service-Oriented Architecture,SOA),提供跨網絡的服務和資源共享。
- 移動應用程序開發:通過Web服務為移動應用程序提供數據和功能支持,與后端服務器進行通信和交互。
- 云計算:在云平臺上部署和管理Web服務,提供云端服務和資源。
1.5. Web服務標準和技術
一些常見的Web服務標準和技術包括:
- SOAP(Simple Object Access Protocol):用于構建基于XML的Web服務的協議。
- WSDL(Web Services Description Language):用于描述Web服務的接口和操作的XML格式的語言。
- UDDI(Universal Description, Discovery, and Integration):用于注冊和發現Web服務的協議和規范。
- REST(Representational State Transfer):一種基于HTTP協議的軟件架構風格,用于構建RESTful Web服務。
- JSON(JavaScript Object Notation):一種輕量級的數據交換格式,通常用于RESTful Web服務的數據格式。
2.案例-WebServiceDemo
2.1.引入配置文件
首先,我們需要在項目中添加必要的依賴項。這些依賴項將幫助我們集成Apache CXF到Spring Boot應用程序中。我的使用的是gradle構建的項目
// 引入WebServiceimplementation '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.創建接口
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.創建接口實現類
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 學生信息*/@Overridepublic 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.創建WebService配置類
我們需要配置CXF和發布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 {// 創建一個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() {// 創建一個ServletRegistrationBean,將CXFServlet映射到指定路徑ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/wbs/*");return wbsServlet;}// 定義WebService端點@Beanpublic Endpoint endpointPurchase(SpringBus springBus, StudentService studentService) {// 創建EndpointImpl對象,并將SpringBus和WebService實現類傳入EndpointImpl endpoint = new EndpointImpl(springBus, studentService);// 將端點發布到指定路徑endpoint.publish("/user-server");// 打印發布成功消息,顯示服務的訪問地址System.out.println("服務發布成功!地址為: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){// 創建JaxWsDynamicClientFactory實例,用于動態創建客戶端JaxWsDynamicClientFactory proxyFactoryBean = JaxWsDynamicClientFactory.newInstance();// 使用動態客戶端工廠創建客戶端對象,并指定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;}
}