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

作者:Good kid. 时间:2023-04-02 06:06:38 

引入腾讯云依赖


<!--腾讯云核心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整合腾讯云短信开箱即用的示例代码

来源:https://blog.csdn.net/MTQ851/article/details/115011556

标签:springboot,整合,腾讯云,短信
0
投稿

猜你喜欢

  • java使用动态代理来实现AOP(日志记录)的实例代码

    2023-11-28 22:34:45
  • Android使用AutoCompleteTextView实现自动填充功能的案例

    2023-03-26 06:56:47
  • android自定义组件实现仪表计数盘

    2023-12-23 21:27:41
  • c#转义字符串中的所有正则特殊字符方法示例

    2021-06-23 12:05:21
  • 详解Java 自动装箱与拆箱的实现原理

    2022-08-16 11:35:51
  • SpringBoot控制配置类加载顺序方式

    2022-08-18 04:31:15
  • 详解升级Android Studio3.0时遇到的几个问题

    2021-11-19 08:13:52
  • Android中ProgressDialog的dismiss()与cancel()方法的区别

    2021-07-27 04:14:24
  • Kotlin开发笔记之委托属性与区间(译)

    2022-07-12 19:14:32
  • Java中PriorityQueue实现最小堆和最大堆的用法

    2022-03-25 14:32:18
  • c++中的malloc底层实现代码

    2023-11-02 19:49:18
  • springboot如何配置定时任务

    2021-06-22 09:16:12
  • C#中File类的文件操作方法详解

    2022-10-04 01:25:28
  • javaweb学习总结——使用JDBC处理MySQL大数据

    2022-10-19 22:45:32
  • idea打包java可执行jar包的实现步骤

    2022-07-30 02:57:48
  • c#通过unicode编码判断字符是否为中文示例分享

    2022-01-13 16:33:44
  • insert语句太长用StringBuilder优化一下

    2023-03-29 17:56:44
  • C#数值转换-显式数值转换表(参考)

    2023-05-26 22:26:15
  • Android自定义属性 format的深入解析

    2022-12-05 14:56:57
  • 教你创建一个带诊断工具的.NET镜像

    2021-09-28 03:11:11
  • asp之家 软件编程 m.aspxhome.com