網站首頁 編程語言 正文
maven依賴
<!--webservice相關jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.1</version>
</dependency>
webservice config配置
package *;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* 注意:
* org.apache.cxf.Bus
* org.apache.cxf.bus.spring.SpringBus
* org.apache.cxf.jaxws.EndpointImpl
* javax.xml.ws.Endpoint
*/
@Configuration
public class WebServiceConfig {
@Autowired
private SmsService smsService;
@Autowired
private EmailService emailService;
@Value("${webservice.api.url}")
private String webserviceApiUrl;
@Value("${webservice.api.email}")
private String webserviceApiEmail;
/**
* Apache CXF 核心架構是以BUS為核心,整合其他組件。
* Bus是CXF的主干, 為共享資源提供一個可配置的場所,作用類似于Spring的ApplicationContext,這些共享資源包括
* WSDl管理器、綁定工廠等。通過對BUS進行擴展,可以方便地容納自己的資源,或者替換現有的資源。默認Bus實現基于Spring架構,
* 通過依賴注入,在運行時將組件串聯起來。BusFactory負責Bus的創建。默認的BusFactory是SpringBusFactory,對應于默認
* 的Bus實現。在構造過程中,SpringBusFactory會搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。
* 根據這些配置文件構建一個ApplicationContext。開發者也可以提供自己的配置文件來定制Bus。
*/
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 此方法作用是改變項目中服務名的前綴名,此處127.0.0.1或者localhost不能訪問時,請使用ipconfig查看本機ip來訪問
* 此方法被注釋后, 即不改變前綴名(默認是services), wsdl訪問地址為 http://127.0.0.1:8080/services/ws/api?wsdl
* 去掉注釋后wsdl訪問地址為:http://127.0.0.1:8080/soap/ws/api?wsdl
* http://127.0.0.1:8080/soap/列出服務列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看實際的服務
* 新建Servlet記得需要在啟動類添加注解:@ServletComponentScan
*
* 如果啟動時出現錯誤:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
* 可能是springboot與cfx版本不兼容。
* 同時在spring boot2.0.6之后的版本與xcf集成,不需要在定義以下方法,直接在application.properties配置文件中添加:
* cxf.path=/service(默認是services)
*/
@Bean
public ServletRegistrationBean dispatcherServlet2() {
return new ServletRegistrationBean(new CXFServlet(), "/services/*");
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), smsService);
endpoint.setPublishedEndpointUrl(webserviceApiUrl);
endpoint.publish("/ws/api");
return endpoint;
}
@Bean
public Endpoint mailEndpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), emailService);
endpoint.setPublishedEndpointUrl(webserviceApiEmail);
endpoint.publish("/ws/api/emailService");
return endpoint;
}
}
package *;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(name = "SmsService", targetNamespace = "http://sms.webservice.com")
public interface SmsService {
@WebMethod
String saveSmsInfo(@WebParam(name = "data", targetNamespace = "http://sms.webservice.com") String data);
}
package *;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* @auther: wwh
* @date: 2020-11-11 09:34
* @description:
*/
@WebService(name = "EmailService", targetNamespace = "http://email.webservice.com")
public interface EmailService {
@WebMethod
String saveEmailInfo(@WebParam(name = "data", targetNamespace = "http://email.webservice.com") String data);
}
package *;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* WebService涉及到的有這些 "四解三類 ", 即四個注解,三個類
* @WebMethod
* @WebService
* @WebResult
* @WebParam
* SpringBus
* Endpoint
* EndpointImpl
*
* 一般我們都會寫一個接口,然后再寫一個實現接口的實現類,但是這不是強制性的
* @WebService 注解表明是一個webservice服務。
* name:對外發布的服務名, 對應于<wsdl:portType name="ServerServiceDemo"></wsdl:portType>
* targetNamespace:命名空間,一般是接口的包名倒序, 實現類與接口類的這個配置一定要一致這種錯誤
* Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name xxxx
* 對應于targetNamespace="http://server.webservice.example.com"
* endpointInterface:服務接口全路徑(如果是沒有接口,直接寫實現類的,該屬性不用配置), 指定做SEI(Service EndPoint Interface)服務端點接口
* serviceName:對應于<wsdl:service name="ServerServiceDemoImplService"></wsdl:service>
* portName:對應于<wsdl:port binding="tns:ServerServiceDemoImplServiceSoapBinding" name="ServerServiceDemoPort"></wsdl:port>
*
* @WebMethod 表示暴露的服務方法, 這里有接口ServerServiceDemo存在,在接口方法已加上@WebMethod, 所以在實現類中不用再加上,否則就要加上
* operationName: 接口的方法名
* action: 沒發現又什么用處
* exclude: 默認是false, 用于阻止將某一繼承方法公開為web服務
*
* @WebResult 表示方法的返回值
* name:返回值的名稱
* partName:
* targetNamespace:
* header: 默認是false, 是否將參數放到頭信息中,用于保護參數,默認在body中
*
* @WebParam
* name:接口的參數
* partName:
* targetNamespace:
* header: 默認是false, 是否將參數放到頭信息中,用于保護參數,默認在body中
* model:WebParam.Mode.IN/OUT/INOUT
*/
@Component
@WebService(name = "EmailService", targetNamespace = "http://email.webservice.com",
endpointInterface = "*.service.webservice.service.EmailService")
@Slf4j
public class EmailServiceImpl implements EmailService {
@Autowired
SmartMsgService smartMsgService;
@Override
public String saveEmailInfo(@WebParam(name = "data", targetNamespace = "http://email.webservice.com") String data) {
try {
log.info("\n【webservice 發送郵件消息開始】 data:{}",data);
if(StringUtils.isEmpty(data)){
throw new RuntimeException("內容不能為空");
}
JSONObject smsInfo = JSON.parseObject(data);
String msg_id = smartMsgService.sendWebserviceMail(smsInfo);
log.info("\n【webservice 發送郵件消息成功】 msg_id:{}",msg_id);
return ResultVO.builder().msg("發送成功").result(true).msg_id(msg_id).build().toJSONStr();
} catch (RuntimeException e) {
e.printStackTrace();
log.error("\n【webservice 發送郵件消息失敗】 e:{}",e.getMessage());
return ResultVO.builder().msg(e.getMessage()).result(false).build().toJSONStr();
}
}
}
package *;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* WebService涉及到的有這些 "四解三類 ", 即四個注解,三個類
* @WebMethod
* @WebService
* @WebResult
* @WebParam
* SpringBus
* Endpoint
* EndpointImpl
*
* 一般我們都會寫一個接口,然后再寫一個實現接口的實現類,但是這不是強制性的
* @WebService 注解表明是一個webservice服務。
* name:對外發布的服務名, 對應于<wsdl:portType name="ServerServiceDemo"></wsdl:portType>
* targetNamespace:命名空間,一般是接口的包名倒序, 實現類與接口類的這個配置一定要一致這種錯誤
* Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name xxxx
* 對應于targetNamespace="http://server.webservice.example.com"
* endpointInterface:服務接口全路徑(如果是沒有接口,直接寫實現類的,該屬性不用配置), 指定做SEI(Service EndPoint Interface)服務端點接口
* serviceName:對應于<wsdl:service name="ServerServiceDemoImplService"></wsdl:service>
* portName:對應于<wsdl:port binding="tns:ServerServiceDemoImplServiceSoapBinding" name="ServerServiceDemoPort"></wsdl:port>
*
* @WebMethod 表示暴露的服務方法, 這里有接口ServerServiceDemo存在,在接口方法已加上@WebMethod, 所以在實現類中不用再加上,否則就要加上
* operationName: 接口的方法名
* action: 沒發現又什么用處
* exclude: 默認是false, 用于阻止將某一繼承方法公開為web服務
*
* @WebResult 表示方法的返回值
* name:返回值的名稱
* partName:
* targetNamespace:
* header: 默認是false, 是否將參數放到頭信息中,用于保護參數,默認在body中
*
* @WebParam
* name:接口的參數
* partName:
* targetNamespace:
* header: 默認是false, 是否將參數放到頭信息中,用于保護參數,默認在body中
* model:WebParam.Mode.IN/OUT/INOUT
*/
@Component
@WebService(name = "SmsService", targetNamespace = "http://sms.webservice.com",
endpointInterface = "*.service.webservice.service.SmsService")
@Slf4j
public class SmsServiceImpl implements SmsService {
@Autowired
SmartMsgService smartMsgService;
@Override
public String saveSmsInfo(@WebParam(name = "data", targetNamespace = "http://sms.webservice.com") String data) {
try {
log.info("\n【webservice 發送短信消息開始】 data:{}",data);
if(StringUtils.isEmpty(data)){
throw new RuntimeException("內容不能為空");
}
JSONObject smsInfo = JSON.parseObject(data);
String msg_id = smartMsgService.sendWebserviceSms(smsInfo);
log.info("\n【webservice 發送短信消息成功】 msg_id:{}",msg_id);
return ResultVO.builder().msg("發送成功").result(true).msg_id(msg_id).build().toJSONStr();
} catch (RuntimeException e) {
e.printStackTrace();
log.error("\n【webservice 發送短信消息失敗】 e:{}",e.getMessage());
return ResultVO.builder().msg(e.getMessage()).result(false).build().toJSONStr();
}
}
}
原文鏈接:https://blog.csdn.net/sinat_17618381/article/details/110128130
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-09-15 C#?List的賦值問題的解決_C#教程
- 2023-07-02 Python配置文件管理之ini和yaml文件讀取的實現_python
- 2022-02-13 使用filter過濾器計算數組中符合條件的長度
- 2022-11-17 Android?Compose?屬性動畫使用探索詳解_Android
- 2023-07-14 react 利用antd-mobile實現樓層效果
- 2022-11-08 uni-app在手機上打開背景圖片不顯示
- 2022-09-07 redis?protocol通信協議及使用詳解_Redis
- 2022-06-24 python學習之讀取配置文件_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支