首页 > 作文

关于springboot集成阿里云短信的问题

更新时间:2023-04-03 22:31:33 阅读: 评论:0

目录
1.获取签名与模板2.编写模板与签名的枚举类3.配置类4.测试类

1.获取签名与模板

进入阿里云平台,进入短信服务模块,在以下位置添加签名和模板(格式一定按照要求填写 审批的比较严格)

2.编写模板与签名的枚举类

在上文获取模板与签名成功后,并作为常量放在枚举类中。

public enum dysmnum {    /**     * 本类此处需要修改(短信模板编码,签名,参数)     */    login_template_code("sms_187570276", "自定义软件框架集成百度云", "code");    /**     * 短信模板编码     */    private string templatecode;    /**     * 签名     */    private string signname;    /**     * 短信模板必需的数据名称,多个key以逗号分隔,此处配置作为校验     */    private string keys;    private dysmnum(string templatecode, string signname, string keys) {        this.templatecode = templatecode;        this.signname = signname;        this.keys = keys;    }    public string gettemplatecode() {        return templatecode;    }    public void ttemplatecode(string templatecode) {        this.templatecode = templatecode;    }    public string getsignname() {        return signname;    }    public void tsignname(string signname) {        this.signname = signname;    }    public string getkeys() {        return keys;    }    public void tkeys(string keys) {        this.keys = keys;    }    public static dysmnum toenum(string templatecode) {        if (stringutils.impty(templatecode)) {            return null;        }        for (dysmnum item : dysmnum.values()) {            if (item.gettemplatecode().equals(templatecode)) {                return item;            }        }        return null;    }}

3.配置类

这里面需要两个参数accesskeyid,accesskeycret,需要在以下位置获取。

import com.alibaba.fastjson.jsonobject;import com.aliyuncs.defaultacsclient;import com.aliyuncs.iacsclient;import com.aliyuncs.dysmsapi.model.v20170525.ndsmsre我心中的明星作文quest;import com.aliyuncs.dysmsapi.model.v20170525.ndsmsrespon;import com.aliyuncs.exceptions.clientexception;import com.aliyuncs.profile.defaultprofile;import com.aliyuncs.profile.iclientprofile;import com.vanpeng.systemportal.modules.dysms.constant.dysmnum;import org.slf4j.logger;import org.slf4j.loggerfactory;import org.springframework.stereotype.component;@componentpublic class dysmshelper {    private final static logger logger = loggerfactory.getlogger(dysmshelper.class);    //产品名称:云通信短信api产品,无需替换    static final string product = "dysmsapi";    //产品域名,无需替换    static final string domain = "dysmsapi.aliyuncs.com";    //此处需要替换成开发者自己的ak(在阿里云访问控制台寻找 本类需要修改此处)    static  string accesskeyid;    static  string accesskeycret;    public static void taccesskeyid(string accesskeyid) {        dysmshelper.accesskeyid = accesskeyid;    }    public static void taccesskeycret防脱发(string accesskeycret) {        dysmshelper.accesskeycret = accesskeycret;    }    public static string getaccesskeyid() {        return accesskeyid;    }    public static string getaccesskeycret() {        return accesskeycret;    }    public boolean ndsms(string phone, jsonobject templateparamjson, dysmnum dysmnum) throws clientexception {        //可自助调整超时时间        system.tproperty("sun.net.client.defaultconnecttimeout", "10000");        system.tproperty("sun.net.client.defaultreadtimeout", "10000");        //初始化acsclient,暂不支持region化        iclientprofile profile = defaultprofile.getprofile("cn-hangzhou", accesskeyid, accesskeycret);        defaultprofile.addendpoint("cn-hangzhou", "cn-hangzhou", product, domain);        iacsclient acsclient = new defaultacsclient(profile);        //验证json参数        validateparam(templateparamjson, dysmnum);        //组装请求对象-具体描述见控制台-文档部分内容        ndsmsrequest request = new ndsmsrequest();        //必填:待发送手机号        request.tphonenumbers(phone);        //必填:短信签名-可在短信控制台中找到        request.tsignname(dysmnum.getsignn摩尔庄园冰世纪大电影ame());        //必填:短信模板-可在短信控制台中找到        request.ttemplatecode(dysmnum.gettemplatecode());        //可选:模板中的变量替换json串,如模板内容为"亲爱的${name},您的验证码为$[code]"时,此处的值为        request.ttemplateparam(templateparamjson.tojsonstring());        //选填-抒情歌曲排行榜上行短信扩展码(无特殊需求用户请忽略此字段)        //request.tsmsupextendcode("90997");        //可选:outid为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者        //request.toutid("youroutid");        boolean result = fal;        //hint 此处可能会抛出异常,注意catch        ndsmsrespon ndsmsrespon = acsclient.getacsrespon(request);        logger.info("短信接口返回的数据----------------");        logger.info("{code:" + ndsmsrespon.getcode() + ",message:" + ndsmsrespon.getmessage() + ",requestid:" + ndsmsrespon.getrequestid() + ",bizid:" + ndsmsrespon.getbizid() + "}");        if ("ok".equals(ndsmsrespon.getcode())) {            result = true;        }        return result;    }    private static void validateparam(jsonobject templateparamjson, dysmnum dysmnum) {        string keys = dysmnum.getkeys();        string[] keyarr = keys.split(",");        fo节日的英语r (string item : keyarr) {            if (!templateparamjson.containskey(item)) {                throw new runtimeexception("模板缺少参数:" + item);            }        }    }}

4.测试类

使用postman或者其他方式调用即可发送短信。

@restcontroller@requestmapping("/ndsmscontrollor")public class ndsmscontrollor {    @autowired    dysmshelper dysmshelper;    @requestmapping("/ndsms")    public void ndsms() throws clientexception {        jsonobject obj = new jsonobject();        obj.put("code", "1234");        dysmshelper.ndsms("电话号", obj, dysmnum.login_template_code);    }}

注意:一定要往账户中充钱或者办理套餐,否则短信发不出去的。

到此这篇关于springboot集成阿里云短信的文章就介绍到这了,更多相关springboot阿里云短信内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-03 22:31:31,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/bdea1a19ec62cf636a2e1b223a7bd458.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:关于springboot集成阿里云短信的问题.doc

本文 PDF 下载地址:关于springboot集成阿里云短信的问题.pdf

标签:模板   短信   阿里   控制台
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图