springboot整合騰訊云短信開箱即用的示例代碼
更新時間:2021年03月21日 13:43:26 作者:Good kid.
這篇文章主要介紹了springboot整合騰訊云短信開箱即用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
引入騰訊云依賴
<!--騰訊云核心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; // 短信應(yīng)用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 用戶手機(jī)號 * @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ù)未提供或者為空時,會使用默認(rèn)簽名發(fā)送短信 System.out.print(result); return result.errMsg; //OK } catch (HTTPException e) { // HTTP響應(yīng)碼錯誤 log.info("短信發(fā)送失敗,HTTP響應(yīng)碼錯誤!!!!!!!!!!!!"); // e.printStackTrace(); } catch (JSONException e) { // json解析錯誤 log.info("短信發(fā)送失敗,json解析錯誤!!!!!!!!!!!!"); //e.printStackTrace(); } catch (IOException e) { // 網(wǎng)絡(luò)IO錯誤 log.info("短信發(fā)送失敗,網(wǎng)絡(luò)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ù)為手機(jī)號,第二個發(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)文章
使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫
這篇文章主要介紹了使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(詳解)
下面小編就為大家?guī)硪黄趕pring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Java得到一個整數(shù)的絕對值,不使用任何判斷和比較語句,包括API
Java得到一個整數(shù)的絕對值,不使用任何判斷和比較語句,包括API2009-09-09