欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > SpringBoot集成各种短信服务供应商

SpringBoot集成各种短信服务供应商

2024/11/30 1:07:46 来源:https://blog.csdn.net/weixin_39311781/article/details/141960871  浏览:    关键词:SpringBoot集成各种短信服务供应商

一、背景

在集成各种短信服务时,大体实现逻辑和配置基本上是一致的,往往都是做一些重复的工作,这里使用开源的SMS4J。

官方文档:https://sms4j.com/

二、实现

2.1 引入依赖

<!-- 短信 -->
<dependency><groupId>org.dromara.sms4j</groupId><artifactId>sms4j-spring-boot-starter</artifactId><version>3.2.1</version>
</dependency>

2.2 基本配置

sms:# 标注从yml读取配置config-type: yamlblends:# 自定义的标识,就是configIdaliyun1:# 厂商标识supplier: alibaba# accessKeyaccess-key-id: xxx# accessKeySecretaccess-key-secret: xxx# 短信签名signature: xxx# 模板ID 非必须配置,如果使用sendMessage的快速发送需此配置template-id: xxx# sdkAppIdsdk-app-id:# 自定义的标识,就是configIdaliyun2:# 厂商标识supplier: alibaba# accessKeyaccess-key-id: xxx# accessKeySecretaccess-key-secret: xxx# 短信签名signature: xxx# 模板ID 非必须配置,如果使用sendMessage的快速发送需此配置template-id: xxx# sdkAppIdsdk-app-id:

2.3 API使用

package com.ozo.common.sms.service;import org.dromara.sms4j.api.callback.CallBack;import java.util.LinkedHashMap;
import java.util.List;/*** 短信服务类** @author qiangesoft* @date 2024-09-06*/
public interface SmsService {/*** 获取供应商标识** @param configId* @return*/String getSupplier(String configId);/*** 发送短信** @param configId* @param phone* @param message* @return*/boolean sendMessage(String configId, String phone, String message);/*** 发送短信** @param configId* @param phone* @param messages* @return*/boolean sendMessage(String configId, String phone, LinkedHashMap<String, String> messages);/*** 发送短信** @param configId* @param phone* @param templateId* @param messages* @return*/boolean sendMessage(String configId, String phone, String templateId, LinkedHashMap<String, String> messages);/*** 异步发送短信** @param configId* @param phone* @param message* @return*/void sendMessageAsync(String configId, String phone, String message);/*** 异步发送短信** @param configId* @param phone* @param templateId* @param messages* @return*/void sendMessageAsync(String configId, String phone, String templateId, LinkedHashMap<String, String> messages);/*** 异步发送短信** @param configId* @param phone* @param message* @param callBack* @return*/void sendMessageAsync(String configId, String phone, String message, CallBack callBack);/*** 异步发送短信** @param configId* @param phone* @param templateId* @param messages* @param callBack* @return*/void sendMessageAsync(String configId, String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack);/*** 批量发送短信** @param configId* @param phones* @param message* @return*/boolean sendMassMessage(String configId, List<String> phones, String message);/*** 批量发送短信** @param configId* @param phones* @param templateId* @param messages* @return*/boolean sendMassMessage(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages);/*** 异步批量发送短信** @param configId* @param phones* @param message* @return*/void sendMassMessageAsync(String configId, List<String> phones, String message);/*** 异步批量发送短信** @param configId* @param phones* @param templateId* @param messages* @return*/void sendMassMessageAsync(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages);/*** 异步批量发送短信** @param configId* @param phones* @param message* @param callBack* @return*/void sendMassMessageAsync(String configId, List<String> phones, String message, CallBack callBack);/*** 异步批量发送短信** @param configId* @param phones* @param templateId* @param messages* @param callBack* @return*/void sendMassMessageAsync(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages, CallBack callBack);/*** 发送延迟短信** @param configId* @param phone* @param message* @param delayedTime* @return*/void sendDelayedMessage(String configId, String phone, String message, Long delayedTime);/*** 发送延迟短信** @param configId* @param phone* @param templateId* @param messages* @param delayedTime* @return*/void sendDelayedMessage(String configId, String phone, String templateId, LinkedHashMap<String, String> messages, Long delayedTime);/*** 批量发送延迟短信** @param configId* @param phones* @param message* @param delayedTime* @return*/void sendMassDelayedMessage(String configId, List<String> phones, String message, Long delayedTime);/*** 批量发送延迟短信** @param configId* @param phones* @param templateId* @param messages* @param delayedTime* @return*/void sendMassDelayedMessage(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages, Long delayedTime);}
package com.ozo.common.sms.service;import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.callback.CallBack;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.stereotype.Service;import java.util.LinkedHashMap;
import java.util.List;/*** 短信服务类** @author qiangesoft* @date 2024-09-06*/
@Service
public class SmsServiceImpl implements SmsService {@Overridepublic String getSupplier(String configId) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);return smsBlend.getSupplier();}@Overridepublic boolean sendMessage(String configId, String phone, String message) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);SmsResponse smsResponse = smsBlend.sendMessage(phone, message);return smsResponse.isSuccess();}@Overridepublic boolean sendMessage(String configId, String phone, LinkedHashMap<String, String> messages) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);SmsResponse smsResponse = smsBlend.sendMessage(phone, messages);return smsResponse.isSuccess();}@Overridepublic boolean sendMessage(String configId, String phone, String templateId, LinkedHashMap<String, String> messages) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);SmsResponse smsResponse = smsBlend.sendMessage(phone, templateId, messages);return smsResponse.isSuccess();}@Overridepublic void sendMessageAsync(String configId, String phone, String message) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);smsBlend.sendMessageAsync(phone, message);}@Overridepublic void sendMessageAsync(String configId, String phone, String templateId, LinkedHashMap<String, String> messages) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);smsBlend.sendMessageAsync(phone, templateId, messages);}@Overridepublic void sendMessageAsync(String configId, String phone, String message, CallBack callBack) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);smsBlend.sendMessageAsync(phone, message, callBack);}@Overridepublic void sendMessageAsync(String configId, String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);smsBlend.sendMessageAsync(phone, templateId, messages, callBack);}@Overridepublic boolean sendMassMessage(String configId, List<String> phones, String message) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);SmsResponse smsResponse = smsBlend.massTexting(phones, message);return smsResponse.isSuccess();}@Overridepublic boolean sendMassMessage(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);SmsResponse smsResponse = smsBlend.massTexting(phones, templateId, messages);return smsResponse.isSuccess();}@Overridepublic void sendMassMessageAsync(String configId, List<String> phones, String message) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);for (String phone : phones) {smsBlend.sendMessageAsync(phone, message);}}@Overridepublic void sendMassMessageAsync(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);for (String phone : phones) {smsBlend.sendMessageAsync(phone, templateId, messages);}}@Overridepublic void sendMassMessageAsync(String configId, List<String> phones, String message, CallBack callBack) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);for (String phone : phones) {smsBlend.sendMessageAsync(phone, message, callBack);}}@Overridepublic void sendMassMessageAsync(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);for (String phone : phones) {smsBlend.sendMessageAsync(phone, templateId, messages, callBack);}}@Overridepublic void sendDelayedMessage(String configId, String phone, String message, Long delayedTime) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);smsBlend.delayedMessage(phone, message, delayedTime);}@Overridepublic void sendDelayedMessage(String configId, String phone, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);smsBlend.delayedMessage(phone, templateId, messages, delayedTime);}@Overridepublic void sendMassDelayedMessage(String configId, List<String> phones, String message, Long delayedTime) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);smsBlend.delayMassTexting(phones, message, delayedTime);}@Overridepublic void sendMassDelayedMessage(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);smsBlend.delayMassTexting(phones, templateId, messages, delayedTime);}}

2.4 测试

package com.ozo.common.sms.controller;import com.ozo.common.sms.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RequestMapping("/sms")
@RestController
public class SmsController {@Autowiredprivate SmsService smsService;@GetMapping("/send")public Boolean send() {// 此处的configId与配置文件的configId保持一致return smsService.sendMessage("aliyun1", "13111111111", "hahhahah");}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com