SpringBoot創(chuàng)建WebService方法詳解
開發(fā)環(huán)境: IDEA 2022.1.4
1. 概述
雖然webservice這塊使用很少,但在局域網(wǎng)作服務(wù)還是相當(dāng)不錯(cuò)。今天突生想法,想做一個(gè)來試試,查閱百度資料,親手實(shí)踐下。方便操作,便在現(xiàn)有得SpringBoot項(xiàng)目上,來實(shí)踐下,也順便試試Spring web服務(wù)與webservice功能是否可以一起發(fā)布。
我主要添加了三個(gè)java文件,一個(gè)接口IWebService,一個(gè)接口實(shí)現(xiàn)類IWebServiceImpl,一個(gè)配置類CxfConfig。

2. 實(shí)現(xiàn)步驟
2.1 POM現(xiàn)加依賴
<!-- WebSevice -->
<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.2 定義接口 IWebService
@WebService(name = "WebService", targetNamespace = "service.xtjk.ceaning.com")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface IWebService {
@WebMethod
String BedInfo() throws JsonProcessingException;
@WebMethod
Result<?> DeptInfo();
@WebMethod
Result<?> UserInfo();
@WebMethod
Result<?> WardInfo();
@WebMethod
Result<?> OrderInfo(@WebParam(name = "param") String param);
@WebMethod
Result<?> InPatiengInfo(@WebParam(name = "param") String param);
@WebMethod
Result<?> SaveGlu(@WebParam(name = "param") String param);
}2.3 創(chuàng)建類 IWebServiceImpl 并實(shí)現(xiàn)接口 IWebService
@Slf4j
@Component
@WebService(name = "WebService",
targetNamespace = "service.xtjk.ceaning.com",
endpointInterface = "com.ceaning.xtjk.service.IWebService")
public class IWebServiceImpl implements IWebService {
@Autowired
private DictionaryMapper dictionaryMapper;
@Autowired
private OrderMapper orderMapper;
@Autowired
private InPatientMapper inPatientMapper;
@Autowired
private ResultMapper resultMapper;
@Override
public String BedInfo() throws JsonProcessingException {
ObjectMapper obj= new ObjectMapper();
List<Bed> list= null;
try{
list= dictionaryMapper.getBedInfo();
return obj.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return obj.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String DeptInfo() throws JsonProcessingException {
ObjectMapper obj= new ObjectMapper();
List<Dept> list= null;
try{
list= dictionaryMapper.getDeptInfo();
return obj.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return obj.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String UserInfo() throws JsonProcessingException {
ObjectMapper obj= new ObjectMapper();
List<User> list= null;
try{
list= dictionaryMapper.getUserInfo();
return obj.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return obj.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String WardInfo() throws JsonProcessingException {
ObjectMapper obj= new ObjectMapper();
List<Ward> list= null;
try{
list= dictionaryMapper.getWardInfo();
return obj.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return obj.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String OrderInfo(String param) throws JsonProcessingException {
ObjectMapper mapper= new ObjectMapper();
List<Order> list= null;
try{
OrderParam obj= mapper.readValue(param, OrderParam.class);
list= orderMapper.getList(obj);
return mapper.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return mapper.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String InPatiengInfo(String param) throws JsonProcessingException {
ObjectMapper mapper= new ObjectMapper();
List<InPatient> list= null;
try{
InPatientParam obj= mapper.readValue(param, InPatientParam.class);
list= inPatientMapper.getList(obj);
return mapper.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return mapper.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String SaveGlu(String param) throws JsonProcessingException {
ObjectMapper mapper= new ObjectMapper();
DbResult ret= null;
try{
ResultParam obj= mapper.readValue(param, ResultParam.class);
ret= resultMapper.save(obj);
if (ret.getCode().trim().equals("T")){
return mapper.writeValueAsString(Result.ok(ret.getMessage()));
}
else{
return mapper.writeValueAsString(Result.error(ret.getMessage()));
}
} catch (Exception e){
log.error(e.getMessage());
return mapper.writeValueAsString(Result.exception(e.getMessage()));
}
}
}2.4 配置類 CxfConfig
@Configuration
public class CxfConfig {
@Autowired
private IWebService service;
@Bean
public ServletRegistrationBean disServlet(){
return new ServletRegistrationBean(new CXFServlet(), "/open/xtjk/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus(){
return new SpringBus();
}
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint= new EndpointImpl(springBus(), service);
endpoint.publish("/IWebService");
return endpoint;
}
}2.5 啟動服務(wù)
瀏覽器訪問: http://127.0.0.1:8090/open/xtjk/IWebService?wsdl
需要注意URL路徑和CxfConfig得配置關(guān)聯(lián)

2.6 測試
我使用另一個(gè)電腦安裝得SoapUI進(jìn)行測試,數(shù)據(jù)獲取正常。就不截圖了。
篇幅有限,就不羅列其他得了。Springboot相關(guān)知識網(wǎng)上很多。
到此這篇關(guān)于SpringBoot創(chuàng)建WebService方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot創(chuàng)建WebService內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring boot webService使用方法解析
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
- SpringBoot整合WebService的實(shí)現(xiàn)示例
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot調(diào)用對方webService接口的幾種方法示例
- SpringBoot整合WebService的實(shí)戰(zhàn)案例
- springboot整合webservice使用簡單案例總結(jié)
- Java(Springboot)項(xiàng)目調(diào)用第三方WebService接口實(shí)現(xiàn)代碼
相關(guān)文章
Swagger3.0 整合spring boot2.7x避免swagger2.0與boot2.7沖突
這篇文章主要介紹了Swagger3.0 整合spring boot2.7x避免swagger2.0與boot2.7沖突問題,通過注釋掉2.0引入的倆包,直接引入3.0,文中結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10
SpringBoot項(xiàng)目 文件上傳臨時(shí)目標(biāo)被刪除異常的處理方案
這篇文章主要介紹了SpringBoot項(xiàng)目 文件上傳臨時(shí)目標(biāo)被刪除異常的處理方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring?Boot?微服務(wù)中集成?MyBatis-Plus?與集成原生?MyBatis在配置上的不同
在Spring?Boot微服務(wù)中,MyBatis-Plus通過其Boot?Starter和豐富的配置屬性,極大的簡化了MyBatis的集成和配置工作,尤其是對于全局設(shè)置和常用插件的使用,本文給大家介紹Spring?Boot?微服務(wù)中集成?MyBatis-Plus與集成原生?MyBatis?有哪些配置上的不同,感興趣的朋友一起看看吧2025-04-04
在Struts2中如何將父類屬性序列化為JSON格式的解決方法
本篇文章,小編將為大家介紹關(guān)于在Struts2中如何將父類屬性序列化為JSON格式的解決方法,有需要的朋友可以參考一下2013-04-04
SpringBoot中接收POST參數(shù)的幾種方式詳解
這篇文章主要介紹了SpringBoot中接收POST參數(shù)的幾種方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
springboot結(jié)合websocket聊天室實(shí)現(xiàn)私聊+群聊
本文主要介紹了springboot結(jié)合websocket聊天室實(shí)現(xiàn)私聊+群聊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
elasticsearch節(jié)點(diǎn)間通信的基礎(chǔ)transport啟動過程
這篇文章主要為大家介紹了elasticsearch節(jié)點(diǎn)間通信的基礎(chǔ)transport啟動過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04

