一、首先进入云之讯注册账户
进入开发者控制台,获取对应的请求地址、应用ID、用户ID和鉴权token
进行认证
创建模板
二、Springboot集成短信发送
1、Maven依赖
阿里的开源框架FastJson,用于JSON格式字符串与JSON对象及javaBean之间的转换
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
</dependencies>
2、application.properties
# 短信服务
# 应用ID
message.appId= xxxxxxxxxxxxxxxxxxxx
# 用户sid
message.account.sid= xxxxxxxxxxxxxxxxxxxx
# 鉴权token
message.auth.token= xxxxxxxxxxxxxxxxxxxx
# 请求地址
message.rest.url= https://open.ucpaas.com/ol/sms/{function}
# 默认手机号
message.default.phone = xxxxxxxxxxx
# 默认模板
message.defaule.templateId = xxxxxx
功能 | 接口 | 请求地址 | 描述 |
---|---|---|---|
发送短信 | 指定模板单发 | https://open.ucpaas.com/ol/sms/sendsms | 指定变量/固定模板单发 |
~ | 指定模板群发 | https://open.ucpaas.com/ol/sms/sendsms_batch | 指定变量/固定模板群发 |
状态报告 | 推送状态报告 | / | 实时推送短信发送状态报告。 |
模板接口 | 增加模板 | https://open.ucpaas.com/ol/sms/addsmstemplate | 通过接口增加模板 |
~ | 查询模板 | https://open.ucpaas.com/ol/sms/getsmstemplate | 获取账户内的全部模板 |
~ | 编辑模板 | https://open.ucpaas.com/ol/sms/editsmstemplate | 编辑指定模板 |
~ | 删除模板 | https://open.ucpaas.com/ol/sms/deletesmstemplate | 删除指定模板 |
3、配置类
public class MessageConfig implements Serializable {
/**
* 应用ID
*/
@Value("${message.appId}")
private String appId;
/**
* 用户sid
*/
@Value("${message.account.sid}")
private String accountSid;
/**
* 鉴权密钥
*/
@Value("${message.auth.token}")
private String authToken;
/**
* 请求地址
*/
@Value("${message.rest.url}")
private String restUrl;
/**
* 短信模板ID
*/
@Value("${message.default.templateId}")
private String templateId;
/**
* 手机号
*/
@Value("${message.default.phone}")
private String phone;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAccountSid() {
return accountSid;
}
public void setAccountSid(String accountSid) {
this.accountSid = accountSid;
}
public String getAuthToken() {
return authToken;
}
public void setAuthToken(String authToken) {
this.authToken = authToken;
}
public String getRestUrl() {
return restUrl;
}
public void setRestUrl(String restUrl) {
this.restUrl = restUrl;
}
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
4、RestTemplate配置类
/**
* RestTemplate配置类
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
5、SendMessageService
public interface SendMessageService {
/**
* 单发短信
*
* @param messageConfig 短信配置
* @return
*/
String sendMessage(MessageConfig messageConfig);
/**
* 群发短信
*
* @param messageConfig 短信配置
* @return
*/
String sendBatchMessage(MessageConfig messageConfig);
}
6、SendMessageServiceImpl
@Service
public class SendMessageServiceImpl implements SendMessageService {
private static final Logger logger = LoggerFactory.getLogger(SendMessageServiceImpl.class);
@Autowired
private RestTemplate restTemplate;
@Override
public String sendMessage(MessageConfig messageConfig) {
logger.info("sendMessage start,{}", JavaBeanUtil.jacksonToString(messageConfig));
Assert.notNull(messageConfig,"发送短信配置不能为空");
//单发邮件API
String url = messageConfig.getRestUrl()+"sendsms";
//使用restTemplate进行访问远程服务
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> httpEntity = new HttpEntity<String>(getParameter(messageConfig),headers);
return restTemplate.patchForObject(url,httpEntity,String.class);
}
@Override
public String sendBatchMessage(MessageConfig messageConfig) {
logger.info("sendBatchMessage start,{}", JavaBeanUtil.jacksonToString(messageConfig));
Assert.notNull(messageConfig,"发送短信配置不能为空");
//群发邮件API
String url = messageConfig.getRestUrl()+"sendsms_batch";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> httpEntity = new HttpEntity<String>(getParameter(messageConfig),headers);
return restTemplate.patchForObject(url,httpEntity,String.class);
}
/**
* 配置发送参数
*
* @param messageConfig
* @return
*/
public String getParameter(MessageConfig messageConfig) {
JSONObject jsonObject = new JSONObject();
//基础配置
jsonObject.put("sid",messageConfig.getAccountSid());
jsonObject.put("token",messageConfig.getAuthToken());
jsonObject.put("appid",messageConfig.getAppId());
//模板ID 参数之间用逗号作为间隔符
jsonObject.put("template",messageConfig.getTemplateId());
//要发送的手机号,多个手机号用逗号分隔
jsonObject.put("mobile",messageConfig.getPhone());
//用户ID,随状态报告返回,可以不填写
jsonObject.put("uid","");
return JSONObject.toJSONString(jsonObject);
}
}
7、SendMessageController
@RestController
@ResponseResult//用于对接口响应体进行包装的注解
@RequestMapping("/send-message")
@Api(value = "发送短信接口",tags = {"发送短信接口"})
public class SendMessageController {
@Autowired
private SendMessageService sendMessageService;
@PostMapping("/simple")
@ApiOperation(value = "单发消息",notes = "单发手机短信消息")
public String sendMessage(MessageConfig messageConfig) {
return sendMessageService.sendMessage(messageConfig);
}
@PostMapping("/batch")
@ApiOperation(value = "群发消息",notes = "群发手机短信消息")
public String sendBatchMessage(MessageConfig messageConfig) {
return sendMessageService.sendBatchMessage(messageConfig);
}
}