SpringBoot整合WebService服務(wù)的實現(xiàn)代碼
WebService是一個SOA(面向服務(wù)的編程)的架構(gòu),它是不依賴于語言,不依賴于平臺,可以實現(xiàn)不同的語言間的相互調(diào)用,通過Internet進行基于Http協(xié)議的網(wǎng)絡(luò)應(yīng)用間的交互。 其實WebService并不是什么神秘的東西,它就是一個可以遠程調(diào)用的類,或者說是組件,把你本地的功能開放出去共別人調(diào)用。
為什么使用WebService?
簡單解釋一下WebService,比如你的項目需要查詢某銀行賬戶余額。你能直接查嗎,肯定不行,因為數(shù)據(jù)庫是銀行的,他不可能給你權(quán)限。你想訪問他的數(shù)據(jù)庫獲取數(shù)據(jù),這就需要用到WebService。通過調(diào)用銀行暴露的接口來得到你想要的數(shù)據(jù)。
適用場景:
軟件的集成和復(fù)用,如氣象局(服務(wù)端系統(tǒng))、天氣查詢網(wǎng)站等。
- 發(fā)布一個服務(wù)(對內(nèi)/對外),不考慮客戶端類型,不考慮性能,建議WebService
- 服務(wù)端已經(jīng)確定使用webservice,客戶端不能選擇,必須使用WebService
軟件集成:通過遠程調(diào)用技術(shù),將兩個系統(tǒng)整合到一起,從而實現(xiàn)軟件集成。
軟件復(fù)用:同一個款軟件的多次集成,最終實現(xiàn)復(fù)用。
不適用場景:
- 考慮性能時不建議使用WebService
- 同構(gòu)程序下不建議使用WebService
Axis2與CXF的區(qū)別
目前java開發(fā)WebService的框架主要包括Axis2和CXF,如果你需要多語言的支持,你應(yīng)該選擇Axis2。如果你需要把你的實現(xiàn)側(cè)重java并希望和Spring集成,CXF就是更好的選擇,特別是把你的WebService嵌入其他的程序中。
| 區(qū)別 | Axis2 | CXF |
|---|---|---|
| 簡述 | WebService引擎 | 簡易的SOA框架,可以作為ESB |
| spring集成 | 不支持 | 支持 |
| 應(yīng)用集成 | 困難 | 簡單 |
| 是否跨語言 | 是 | 否 |
| 部署方式 | web應(yīng)用 | 嵌入式 |
| 服務(wù)的管控和治理 | 支持 | 不支持 |
SpringBoot使用CXF集成WebService
1.向pom.xml中添加集成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.1.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version> </dependency>
2.創(chuàng)建服務(wù)端接口
package com.primeton.mq.service;
import javax.jws.WebService;
@WebService(name = "DemoService", // 暴露服務(wù)名稱
targetNamespace = "http://service.mq.primeton.com"http:// 命名空間,一般是接口的包名倒序
)
public interface DemoService {
public String sayHello(String user);
}3.創(chuàng)建服務(wù)端接口實現(xiàn)類
package com.primeton.mq.service.impl;
import com.primeton.mq.service.DemoService;
import javax.jws.WebService;
import java.util.Date;
@WebService(serviceName = "DemoService", // 與接口中指定的name一致
targetNamespace = "http://service.mq.primeton.com", // 與接口中的命名空間一致,一般是接口的包名倒
endpointInterface = "com.primeton.mq.service.DemoService"http:// 接口地址
)
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String user) {
return user+",現(xiàn)在時間:"+"("+new Date()+")";
}
}4.創(chuàng)建CXF配置類
package com.primeton.mq.webServiceConfig;
import com.primeton.mq.service.DemoService;
import com.primeton.mq.service.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 dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/demo/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
public DemoService demoService() {
return new DemoServiceImpl();
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api");
return endpoint;
}5.啟動SpringBoot服務(wù)
輸入http://localhost:8090/demo/api?wsdl即可,注意我設(shè)置的端口是8090,視情況而定。

6.創(chuàng)建客戶端
在idea中進入File > New Project…菜單打開新建項目窗口,依次選擇Java、WebServices,Version項選擇Apache Axis,Libraries項選擇 Download。然后點擊Next按鈕進入下一頁。

在下一頁中輸入項目名稱,然后點擊Finish按鈕開始下載依賴包。依賴包下載完成后進入新建的項目。

選WebServices > Generate Wsdl From Java Code,生成wsdl

然后選擇服務(wù)地址

確定之后,創(chuàng)建一個測試類,代碼如下:
package test;
import example.DemoServiceImplService;
public class demo {
public static void main(String[] args) {
DemoServiceImplService webServiceImpl = new DemoServiceImplService();
String result = webServiceImpl.getDemoServiceImplPort().sayHello("沒有說");
System.out.println("===========================================");
System.out.println(result);
}
}注意:實現(xiàn)WebService客戶端不一定要生成客戶端的代碼,可以通過地址動態(tài)調(diào)用。
package com.primeton.mq.test;
import com.primeton.mq.service.DemoService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
public class demo {
public static void main(String[] args) {
//創(chuàng)建動態(tài)客戶端
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient("http://localhost:8090/demo/api?wsdl");
// 需要密碼的情況需要加上用戶名和密碼
//client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(2000); //連接超時
httpClientPolicy.setAllowChunking(false); //取消塊編碼
httpClientPolicy.setReceiveTimeout(120000); //響應(yīng)超時
conduit.setClient(httpClientPolicy);
//client.getOutInterceptors().addAll(interceptors);//設(shè)置攔截器
try{
Object[] objects = new Object[0];
// invoke("方法名",參數(shù)1,參數(shù)2,參數(shù)3....);
objects = client.invoke("sayHello", "sujin");
System.out.println("返回數(shù)據(jù):" + objects[0]);
}catch (Exception e){
e.printStackTrace();
}
}到此這篇關(guān)于SpringBoot整合WebService服務(wù)的文章就介紹到這了,更多相關(guān)SpringBoot整合WebService服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
老生常談spring boot 1.5.4 日志管理(必看篇)
下面小編就為大家?guī)硪黄仙U剆pring boot 1.5.4 日志管理(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
簡單了解springboot中的配置文件相關(guān)知識
這篇文章主要介紹了簡單了解springboot中的配置文件相關(guān)知識,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
JAVA8 List<List<Integer>> list中再裝一個list轉(zhuǎn)成一個list操
這篇文章主要介紹了JAVA8 List<List<Integer>> list中再裝一個list轉(zhuǎn)成一個list操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

