欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot整合腾讯云短信开箱即用的示例代码

程序员文章站 2022-04-23 20:14:46
引入腾讯云依赖 com.tencentcloudapi...

引入腾讯云依赖

 <!--腾讯云核心api-->
 <dependency>
  <groupid>com.tencentcloudapi</groupid>
  <artifactid>tencentcloud-sdk-java</artifactid>
  <version>3.1.111</version>
 </dependency>
 <dependency>
  <groupid>com.github.qcloudsms</groupid>
  <artifactid>qcloudsms</artifactid>
  <version>1.0.6</version>
 </dependency>

其次配置属性类

import lombok.data;
import org.springframework.boot.context.properties.configurationproperties;

/**
 * 腾讯云发送短信 配置信息类
 */
@data
@configurationproperties(prefix = "tanhua.txsms") // 读取application中的tanhua.sms的属性
public class txproperties {
 // appid 1400开头的
 private int appid;
 // 短信应用sdk appkey
 private string appkey;
 // 短信模板id
 private int templateid;
 // 签名
 private string signname;

}

其次配置工具类

package com.he.commons.templates;

import com.he.commons.properties.txproperties;
import com.alibaba.fastjson.jsonexception;
import com.github.qcloudsms.smssinglesender;
import com.github.qcloudsms.smssinglesenderresult;
import com.github.qcloudsms.httpclient.httpexception;
import lombok.extern.slf4j.slf4j;


import java.io.ioexception;


/**
 * 腾讯云发送短信模板对象,封装了发送短信的api
 */
@slf4j
public class txsmstemplate {

 private txproperties txproperties;

 public txsmstemplate(txproperties txproperties) {
 this.txproperties = txproperties;
 }


 /**
 * 指定模板id发送短信
 *
 * @param number 用户手机号
 * @return ok 成功 null 失败
 */
 public string sendmesmodel(string number,string value) {
 try {
  string[] params = {value};//{参数}
  smssinglesender ssender = new smssinglesender(txproperties.getappid(), txproperties.getappkey());
  smssinglesenderresult result = ssender.sendwithparam("86", number,
   txproperties.gettemplateid(), params, txproperties.getsignname(), "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
  system.out.print(result);
  return result.errmsg; //ok
 } catch (httpexception e) {
  // http响应码错误
  log.info("短信发送失败,http响应码错误!!!!!!!!!!!!");
  // e.printstacktrace();
 } catch (jsonexception e) {
  // json解析错误
  log.info("短信发送失败,json解析错误!!!!!!!!!!!!");
  //e.printstacktrace();
 } catch (ioexception e) {
  // 网络io错误
  log.info("短信发送失败,网络io错误!!!!!!!!!!!!");
  // e.printstacktrace();
 }
 return null;
 }
}

注册腾讯云短信到容器中

/**
 * 短信模块自动装配类
 * 根据springboot自动装备原理
 * 将smstemplate对象注入到容器中
 * 要配置meta-inf/spring.factories
 */
@configuration
@enableconfigurationproperties({txproperties.class})
public class commonsautoconfiguration {
 /**
 * 注册txsmstemplate到容器中
 * @param txproperties
 * @return
 */
 @bean
 public txsmstemplate txsmstemplate(txproperties txproperties) {
 return new txsmstemplate(txproperties);
 }
}

在resources中配置启动自动装配类

springboot整合腾讯云短信开箱即用的示例代码

org.springframework.boot.autoconfigure.enableautoconfiguration=\
com.he.commons.commonsautoconfiguration

最后测试一下

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;

@springboottest
@runwith(springrunner.class)
public class txtest {
 @autowired
 private txsmstemplate txsmstemplate;

 @test
 public void testtxsms(){
 string result = txsmstemplate.sendmesmodel("1511938****", "666666");//第一个参数为手机号,第二个发送短信的内容
 system.out.println(result); // result等于ok 就表示发送成功
 }
}

返回ok则表示发送成功
springboot整合腾讯云短信开箱即用的示例代码

到此这篇关于springboot整合腾讯云短信开箱即用的文章就介绍到这了,更多相关springboot整合腾讯云短信内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!