第一步:注册云片的账号;
第二步:在pom文件放入
<dependency> <groupId>com.yunpian.sdk</groupId> <artifactId>yunpian-java-sdk</artifactId> <version>1.2.4</version> </dependency>
第三步:
DictionaryConstants.APIKEY = "
短信接口用户唯一标识:apike
"
public Result<SmsSingleSend> sendSms(String prodId, String mobile, String type, ResourceBundle rb) throws IllegalArgumentException { //验证码 String verCode = ""; //生成6位数验证码 verCode = createRandomVcode(); //取得产品名字 JobResult<BdpProdInfoEntity> bdpProdInfoEntityJobResult = sendTemplateService.getBdpProdnfo(prodId,rb); String prodName = bdpProdInfoEntityJobResult.getModule().getProdName(); //取得数据库的模板内容 JobResult<BdpProdCfgVo> jobResult = sendTemplateService.getBdpProdCfgInfo(prodId, DictionaryConstants.VERIFY_CODE_TEMPLATE,rb); String prodCfgDesc = jobResult.getModule().getProdCfgDesc(); prodCfgDesc = prodCfgDesc.replace("#app#", prodName); prodCfgDesc = prodCfgDesc.replace("#code#", verCode); //初始化client,apikey作为所有请求的默认值(可以为空) YunpianClient clnt = new YunpianClient(DictionaryConstants.APIKEY).init(); //修改账户信息API Map<String, String> param = clnt.newParam(2); param.put(YunpianClient.MOBILE, mobile); param.put(YunpianClient.TEXT, prodCfgDesc); Result<SmsSingleSend> r = clnt.sms().single_send(param); System.out.println(r); //将取得的验证码放到redis缓存中 if (r.getCode() == 0) { RedisVo redisVo = new RedisVo(); redisVo.setVerCode(verCode); //短信发送的时间 redisVo.setLastTime(new Date().getTime()); redisVo.setMobile(mobile); redisVo.setType(type); redisUtils.setKeyValue(type + mobile, redisVo); } //Result<SmsSingleSend> r = clnt.sms().single_send(param); //获取返回结果,返回码:r.getCode(),返回码描述:r.getMsg(),API结果:r.getData(),其他说明:r.getDetail(),调用异常:r.getThrowable() //账户:clnt.user().* 签名:clnt.sign().* 模版:clnt.tpl().* 短信:clnt.sms().* 语音:clnt.voice().* 流量:clnt.flow().* 隐私通话:clnt.call().* //最后释放client clnt.close(); return r; } /** * 随机生成6位随机验证码 * 方法说明 * * @ModifyDate: 2017年8月11日 */ public static String createRandomVcode() { //验证码 String vcode = ""; for (int i = 0; i < 6; i++) { vcode = vcode + (int) (Math.random() * 9); } return vcode; } 具体有问题可以参考云片官方的API,本文章仅作为作者学习用