springboot整合騰訊云短信開箱即用的示例代碼
更新時間:2021年03月21日 13:43:26 作者:Good kid.
這篇文章主要介紹了springboot整合騰訊云短信開箱即用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
引入騰訊云依賴
<!--騰訊云核心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;
/**
* 騰訊云發(fā)送短信 配置信息類
*/
@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;
/**
* 騰訊云發(fā)送短信模板對象,封裝了發(fā)送短信的api
*/
@Slf4j
public class TxSmsTemplate {
private TxProperties txProperties;
public TxSmsTemplate(TxProperties txProperties) {
this.txProperties = txProperties;
}
/**
* 指定模板ID發(fā)送短信
*
* @param number 用戶手機號
* @return OK 成功 null 失敗
*/
public String sendMesModel(String number,String value) {
try {
String[] params = {value};//{參數(shù)}
SmsSingleSender ssender = new SmsSingleSender(txProperties.getAppId(), txProperties.getAppKey());
SmsSingleSenderResult result = ssender.sendWithParam("86", number,
txProperties.getTemplateId(), params, txProperties.getSignName(), "", ""); // 簽名參數(shù)未提供或者為空時,會使用默認簽名發(fā)送短信
System.out.print(result);
return result.errMsg; //OK
} catch (HTTPException e) {
// HTTP響應碼錯誤
log.info("短信發(fā)送失敗,HTTP響應碼錯誤!!!!!!!!!!!!");
// e.printStackTrace();
} catch (JSONException e) {
// json解析錯誤
log.info("短信發(fā)送失敗,json解析錯誤!!!!!!!!!!!!");
//e.printStackTrace();
} catch (IOException e) {
// 網(wǎng)絡IO錯誤
log.info("短信發(fā)送失敗,網(wǎng)絡IO錯誤!!!!!!!!!!!!");
// e.printStackTrace();
}
return null;
}
}
注冊騰訊云短信到容器中
/**
* 短信模塊自動裝配類
* 根據(jù)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中配置啟動自動裝配類

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");//第一個參數(shù)為手機號,第二個發(fā)送短信的內(nèi)容
System.out.println(result); // result等于OK 就表示發(fā)送成功
}
}
返回OK則表示發(fā)送成功

到此這篇關(guān)于springboot整合騰訊云短信開箱即用的文章就介紹到這了,更多相關(guān)springboot整合騰訊云短信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
SpringBoot日程管理Quartz與定時任務Task實現(xiàn)詳解
定時任務是企業(yè)級開發(fā)中必不可少的組成部分,諸如長周期業(yè)務數(shù)據(jù)的計算,例如年度報表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報告,還有搶購類活動的商品上架,這些都離不開定時任務。本節(jié)將介紹兩種不同的定時任務技術(shù)2022-09-09
Springboot通過ObjectMapper配置json序列化詳解
SpringBoot默認集成Jackson庫,其中ObjectMapper類是核心,用于Java對象與JSON字符串的互轉(zhuǎn),提供配置序列化特性、注冊模塊等方法,在SpringBoot中可以全局配置JSON格式,如日期格式化、將Long轉(zhuǎn)為字符串,還可以配置序列化時的各種規(guī)則,感興趣的可以了解一下2024-10-10
Springboot通過谷歌Kaptcha?組件生成圖形驗證碼功能
Kaptcha是谷歌開源的一款簡單實用的圖形驗證碼組件。我個人推薦它的最大原因是容易上手,采用約定大于配置的方式,快速契合到項目中,這篇文章主要介紹了Springboot通過谷歌Kaptcha組件生成圖形驗證碼的方法,需要的朋友可以參考下2023-05-05

