简介
使用SpringBoot
自动装配简化对接阿里云短信过程。
小工具一枚,欢迎使用和Star支持,如使用过程中碰到问题,可以提出Issue,我会尽力完善该Starter。
版本基础
aliyun-java-sdk-core:4.1.0
如何使用
Maven
1 2 3 4 5
| <dependency> <groupId>io.github.gcdd1993</groupId> <artifactId>ali-sms-spring-boot-starter</artifactId> <version>1.0.0.RELEASE</version> </dependency>
|
Gradle
1
| compile 'io.github.gcdd1993:ali-sms-spring-boot-starter:1.0.0.RELEASE'
|
👉注意:需要引入Jcenter
仓库
参数配置
以application.yml
举例
1 2 3 4 5 6 7 8 9 10
| ali: sms: domain: "dysmsapi.aliyuncs.com" version: "2017-05-25" action: "SendSms" access-key: id: "${阿里云短信AccessKeyId}" secret: "${阿里云短信AccessKeySecret}" region-id: "${阿里云短信地域}" sign-name: "${阿里云短信签名}"
|
基本使用
同步发送短信
为了方便使用,接口上进行了方法的重载,提供5种不同的参数列表供选择,你可以自行选择使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
@Test public void sendSync() { SmsResponse smsResponse = sendService.sendSync(TEMPLATE_ID, PHONE_NUMBER, MAP); Assert.assertTrue(smsResponse.isSuccess()); }
@Test public void sendSync1() { SmsResponse smsResponse = sendService.sendSync(TEMPLATE_ID, PHONE_NUMBER, "{\"code\":\"112233\"}"); Assert.assertTrue(smsResponse.isSuccess()); }
@Test public void sendSync2() { SmsResponse smsResponse = sendService.sendSync(SIGN_NAME, TEMPLATE_ID, PHONE_NUMBER, MAP); Assert.assertTrue(smsResponse.isSuccess()); }
@Test public void sendSync3() { SmsResponse smsResponse = sendService.sendSync(SIGN_NAME, TEMPLATE_ID, PHONE_NUMBER, "{\"code\":\"112233\"}"); Assert.assertTrue(smsResponse.isSuccess()); }
|
最后一个提供了一个参数对象来定义短信发送请求,如果不嫌麻烦,可以使用这个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
@Data public class SmsRequest {
private String phoneNumbers;
private String signName;
private Integer templateId;
private Map<String, String> params;
private String paramStr;
}
|
使用:
1 2 3 4 5 6 7 8 9
| @Test public void sendSync4() { SmsRequest smsRequest = new SmsRequest(); smsRequest.setPhoneNumbers(PHONE_NUMBER); smsRequest.setTemplateId(TEMPLATE_ID); smsRequest.setParams(MAP); SmsResponse smsResponse = sendService.sendSync(smsRequest); Assert.assertTrue(smsResponse.isSuccess()); }
|
异步发送短信
考虑到发短信的需求,一般来说都需要异步加持,对以上5种方法分别提供了异步接口sendAsync
,使用方法基本一致,唯一不同的是,你可以异步处理短信发送返回值。
1 2 3 4 5 6 7 8
| CompletableFuture<SmsResponse> smsResponse = sendService.sendAsync(TEMPLATE_ID, PHONE_NUMBER, MAP); smsResponse.thenAcceptAsync(sr -> { if (sr.isSuccess()) { System.out.println("发短信成功"); } else { System.out.println("发送到消息队列,准备重试此次短信"); } });
|
高级使用
除了使用以上方法发送短信外,你还可以使用官方的IAcsClient
来发送短信,如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package io.github.gcdd1993.demo;
import com.aliyuncs.CommonRequest; import com.aliyuncs.IAcsClient; import com.aliyuncs.request; import com.aliyuncs.CommonResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.MethodType; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.gcdd1993.alisms.domain.SmsRequest; import io.github.gcdd1993.alisms.domain.SmsResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
@Service public class SendService {
@Autowired private IAcsClient acsClient;
public SmsResponse sendSync() { try { CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("RegionId", "region"); request.putQueryParameter("PhoneNumbers", "1771636783"); request.putQueryParameter("SignName", "SignName"); request.putQueryParameter("TemplateCode", "SMS_12345678"); request.putQueryParameter("TemplateParam", "{\"code\":\"112233\"}"); CommonResponse commonResponse = acsClient.getCommonResponse(request);
return SmsResponse.SmsResponseBuilder.build(commonResponse);
} catch (ClientException e) { log.error("send msg error.", e); return SmsResponse.SmsResponseBuilder.buildFail(e.getMessage()); } catch (JsonProcessingException e) { log.error("write json failed.", e); return SmsResponse.SmsResponseBuilder.buildFail("短信参数在json序列化时出错"); } } }
|
Licenses
The Apache License, Version 2.0
Issues
Issues Welcome
支持
更多参考
阿里云短信服务API参考