WebService簡介
WebService是一個SOA(面向服務的編程)的架構,它是不依賴於語言,不依賴於平臺,可以實現不同的語言間的相互調用,通過Internet進行基於Http協議的網絡應用間的交互。 其實WebService並不是什麼神祕的東西,它就是一個可以遠程調用的類,或者說是組件,把你本地的功能開放出去供別人調用。
爲什麼使用WebService?
簡單解釋一下WebService,比如你的項目需要查詢第三方項目的數據。你能直接查嗎,肯定不行,因爲數據庫是第三方的,他不可能給你權限。你想訪問他的數據庫獲取數據,這就需要用到WebService。通過調用第三方暴露的接口來得到你想要的數據。
適用場景:
軟件的集成和複用,如氣象局(服務端系統)、天氣查詢網站等。
- 發佈一個服務(對內/對外),不考慮客戶端類型,不考慮性能,建議WebService
- 服務端已經確定使用webservice,客戶端不能選擇,必須使用WebService
不適用場景:
- 考慮性能時不建議使用WebService
- 同架構程序下不建議使用WebService
Axis2與CXF的區別
目前java開發WebService的框架主要包括Axis2和CXF,如果你需要多語言的支持,你應該選擇Axis2。如果你需要把你的實現側重java並希望和Spring集成,CXF就是更好的選擇,特別是把你的WebService嵌入其他的程序中。
先呈上完整項目代碼
WebService服務端
WebService客戶端
編寫WebService服務端
SpringBoot使用CXF集成WebService
1.向pom.xml中添加集成WebService的相關依賴
<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.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
2.創建服務端接口
package com.example.demo.service;
import javax.jws.WebService;
@WebService(name = "demoService",// 暴露服務名稱
targetNamespace = "http://demo.example.com"// 命名空間,一般是接口的包名倒序
)
public interface DemoService {
String sayHello(String name);
}
3.創建服務端接口實現類
package com.example.demo.impl;
import javax.jws.WebService;
import com.example.demo.service.DemoService;
@WebService(serviceName = "demoService", // 與接口中指定的name一致
targetNamespace = "http://demo.example.com", // 與接口中的命名空間一致,一般是接口的包名倒
endpointInterface = "com.example.demo.service.DemoService"// 接口地址
)
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
String said = name + ",Hello~~";
System.out.println(said);
return said;
}
}
4.創建CXF配置類
package com.example.demo.config;
import com.example.demo.service.DemoService;
import com.example.demo.impl.DemoServiceImpl;
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 {
@Bean
public ServletRegistrationBean dispatcherServletDemo() {
return new ServletRegistrationBean(new CXFServlet(), "/demo/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public DemoService demoService() {
return new DemoServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api");
return endpoint;
}
}
5.啓動SpringBoot服務
輸入http://localhost:8080/demo/api?wsdl即可。
編寫WebService客戶端
1、在idea中進入File > New Project…菜單打開新建項目窗口,依次選擇Java、WebServices,Version項選擇Apache Axis,Libraries項選擇 Download。然後點擊Next按鈕進入下一頁。
2、在下一頁中輸入項目名稱,然後點擊Finish
按鈕開始下載依賴包。依賴包下載完成後進入新建的項目。
3、選WebServices > Generate Wsdl From Java Code,生成wsdl
4、然後選擇服務地址
5、確定之後,創建一個測試類,代碼如下:
package test;
public class demo {
public static void main(String[] args) {
try{
DemoService_ServiceLocator webServiceImpl = new DemoService_ServiceLocator();
String result = webServiceImpl.getDemoServiceImplPort().sayHello("WebService測試!");
System.out.println(result);
}catch (Exception e){
e.printStackTrace();
}
}
}
6、查看運行結果
注意:WebService解析方法有兩種,上面從IDEA中解析是一種。還有一種是藉助wsimport命令工具自動生成客戶端代碼。
命令是:wsimport -s D:\zrz_project\src\main\java\com\zrz\create -p com http://localhost:8080/demo/api?wsdl
感興趣的同學可以自行驗證一下,第二種方法我在其他項目中驗證過,可行。