当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot实现发送短信的示例代码

SpringBoot实现发送短信的示例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

一.说明

在 注册账号并且认证为个人开发者(需要身份证),这个不难,不多说了

短信的三方平台有许多,对于选择什么平台要根据个人业务场景选择,这里只是demo

二.创建平台项目

创建平台项目后可以获得短信基础配置,在调用短信接口时使用

三.创建短信模板

模板动态参数设置规则为{1}{2}{3}...{n} (注意:在调用时参数之间拼接用逗号作为间隔符,这个见代码描述)

四.使用resttemplate调用短信接口

spring boot 版本:2.1.3

项目中使用了阿里的开源框架fastjson,用于json格式字符串与json对象及javabean之间的转换 ,maven依赖如下

<dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
      <version>2.1.3.release</version>
    </dependency>
    <dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>fastjson</artifactid>
      <version>1.2.45</version>
    </dependency>
</dependencies>

建立resttemplate配置类,将resttemplate注入容器中

/**
 * resttemplate配置类
 * @author sans
 * @createtime 2019/4/2 09:55
 */
@configuration
public class resttemplateconfig {
  @bean
  public resttemplate resttemplate() {
    return new resttemplate();
  }
}

controller测试

/**
 * 测试短信demo
 * @author sans
 * @createtime 2019/4/2 09:39
 */
@restcontroller
@requestmapping("/sms")
public class testcontroller {

  @autowired
  private resttemplate resttemplate;

  /**
   * 单发短信测试
   * @author: sans
   * @createtime: 2019/4/2 10:06
   */
  @requestmapping(value = "/sendsmstest",method = requestmethod.get)
  public string sendsmstest(){
    //单发短信api
    string url = "https://open.ucpaas.com/ol/sms/sendsms";
    jsonobject jsonobject = new jsonobject();
    //基础配置,在开发平台认证后获取
    jsonobject.put("sid","ad024f8****************05d1614");
    jsonobject.put("token","5ddbf62d4d****************e27402c");
    jsonobject.put("appid","0ceaca4708****************76ec45f");
    //模板id,在开发平台创建模板对应的模板id
    jsonobject.put("templateid", "432116");
    //模板对应的参数,参数之间拼接用逗号作为间隔符
    jsonobject.put("param", "1315,500");
    //要发送的手机号
    jsonobject.put("mobile", "用户的手机号");
    //用户透传id,随状态报告返回,可以不填写
    jsonobject.put("uid","");
    string json = jsonobject.tojsonstring(jsonobject);
    //使用resttemplate进行访问远程服务
    httpheaders headers = new httpheaders();
    headers.setcontenttype(mediatype.application_json_utf8);
    httpentity<string> httpentity = new httpentity<string>(json, headers);
    string result = resttemplate.postforobject(url, httpentity, string.class);
    return result;
  }

  /**
   * 群发短信测试
   * @author: sans
   * @createtime: 2019/4/2 11:23
   */
  @requestmapping(value = "/sendbatchsmstest",method = requestmethod.get)
  public string sendbatchsmstest(){
    //群发短信api
    string url = "https://open.ucpaas.com/ol/sms/sendsms_batch";
    jsonobject jsonobject = new jsonobject();
    //基础配置,在开发平台认证后获取
    jsonobject.put("sid","ad024f8****************05d1614");
    jsonobject.put("token","5ddbf62d4d****************e27402c");
    jsonobject.put("appid","0ceaca4708****************76ec45f");
    //模板id,在开发平台创建模板对应的模板id
    jsonobject.put("templateid", "432116");
    //模板对应的参数,参数之间拼接用逗号作为间隔符
    jsonobject.put("param", "1315,500");
    //群发多个手机号之间要用逗号作为间隔符
    jsonobject.put("mobile", "用户的手机号a,用户的手机号b");
    //用户透传id,随状态报告返回,可以不填写
    jsonobject.put("uid","");
    string json = jsonobject.tojsonstring(jsonobject);
    //使用resttemplate进行访问远程服务
    httpheaders headers = new httpheaders();
    headers.setcontenttype(mediatype.application_json_utf8);
    httpentity<string> httpentity = new httpentity<string>(json, headers);
    string result = resttemplate.postforobject(url, httpentity, string.class);
    return result;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网