1.下载云之讯SDK。
2.将src里的文件拷贝至SpringBoot启动类同级目录。
3.将src里的config.properties剪切至SpringBoot的application.yml同级位置。
4.调整拷贝到SpringBoot的.java文件(可能会因为文件位置报错调整即可),并增加以下所需依赖。
<!-- 云之讯(可能是个人所需,根据报错引入相应依赖便是) -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
5.打开拷贝过来的RestTest.java,这里面已经说明怎样去发送验证码了,下面这段代码便是模板单发方法,需要其他类型的短信可以参照文档截取所需代码。
try {
String result=InstantiationRestAPI().sendSms(sid, token, appid, templateid, param, mobile, uid);
System.out.println("Response content is: " + result);
} catch (Exception e) {
e.printStackTrace();
}
6.Controller
/**
* 验证码
*/
@RequestMapping("/authCode")
public void authCode() throws Exception {
String sid = "";//用户的账号唯一标识“Account Sid”
String token = "";//用户密钥“Auth Token”
String appid = "";//创建应用时系统分配的唯一标示
String templateid = "";//可在后台短信产品→选择接入的应用→短信模板-模板ID,查看该模板ID
String param = generateWord();//模板中的替换参数(验证码)
String mobile = "";//接收的单个手机号,暂仅支持国内号码
String uid = "";//用户透传ID,随状态报告返回
try {
String result=new JsonReqClient().sendSms(sid, token, appid, templateid, param, mobile, uid);
System.out.println("Response content is: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 产生随机的6位数字字符串
*/
private static String generateWord() {
int length = 6;
String[] beforeShuffle = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9"};
List list = Arrays.asList(beforeShuffle);
Collections.shuffle(list);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i));
}
String afterShuffle = sb.toString();
return afterShuffle.substring(2, 2 + length);
}
7.配置完后将config.properties中的is_test改为true。
8.泡壶茶,大功告成。
本文详细介绍如何在SpringBoot项目中集成云之讯SDK,包括下载SDK、调整文件位置、添加依赖、配置发送验证码功能及调整配置文件等步骤。
2278

被折叠的 条评论
为什么被折叠?



