springboot2.x 接入阿里云市場短信發(fā)送的實現(xiàn)
1.短信平臺購買次數(shù)地址
https://market.aliyun.com/products/57000002/cmapi00046920.html
提供測試模板、免審核、測試成本更低
2.測試學習使用的話,3塊錢75多次夠用了
3.購買后在跳轉(zhuǎn)成功頁面記錄
AppSecret、key、code
?4.記錄模板ID
5.上代碼環(huán)節(jié)
@Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory){ return new RestTemplate(requestFactory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(10000); factory.setConnectTimeout(10000); return factory; } }
增加配置
@ConfigurationProperties(prefix = "sms") @Configuration @Data public class SmsConfig { private String templateId; private String appCode; }
application.yml
發(fā)送類
@Component @Slf4j public class SmsComponent { /** * 發(fā)送地址 */ private static final String URL_TEMPLATE = "https://jmsms.market.alicloudapi.com/sms/send?mobile=%s&templateId=%s&value=%s"; @Autowired private RestTemplate restTemplate; @Autowired private SmsConfig smsConfig; /** * 發(fā)送短信驗證碼 * @param to * @param templateId * @param value */ public void send(String to,String templateId,String value){ String url = String.format(URL_TEMPLATE,to,templateId,value); HttpHeaders headers = new HttpHeaders(); headers.set("Authorization","APPCODE "+smsConfig.getAppCode()); HttpEntity entity = new HttpEntity<>(headers); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); log.info("url={},body={}",url,response.getBody()); if(response.getStatusCode().is2xxSuccessful()){ log.info("發(fā)送短信驗證碼成功"); }else { log.error("發(fā)送短信驗證碼失敗:{}",response.getBody()); } } }
測試類驗證
@RunWith(SpringRunner.class) @SpringBootTest(classes = AccountApplication.class) @Slf4j public class SmsTest { @Autowired private SmsComponent smsComponent; @Autowired private SmsConfig smsConfig; @Test public void testSendSms(){ smsComponent.send("138XXXXXXX",smsConfig.getTemplateId(),"666888"); } }
發(fā)送成功咯
到此這篇關于springboot2.x 接入阿里云市場短信發(fā)送的實現(xiàn)的文章就介紹到這了,更多相關springboot 阿里云短信發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Go Java算法之外觀數(shù)列實現(xiàn)方法示例詳解
這篇文章主要為大家介紹了Go Java算法外觀數(shù)列實現(xiàn)的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08SpringBoot中配置Web靜態(tài)資源路徑的方法
這篇文章主要介紹了SpringBoot中配置Web靜態(tài)資源路徑的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09springboot?整合mysql實現(xiàn)版本管理通用最新解決方案
當springboot微服務項目完成從開發(fā)到測試全流程后,通常來說,最終交付產(chǎn)物是一個完整的安裝包,這篇文章主要介紹了springboot?整合mysql實現(xiàn)版本管理通用解決方案,需要的朋友可以參考下2023-08-08idea一鍵部署SpringBoot項目jar包到服務器的實現(xiàn)
我們在開發(fā)環(huán)境部署項目一般通過idea將項目打包成jar包,然后連接linux服務器,將jar手動上傳到服務中,本文就來詳細的介紹一下步驟,感興趣的可以了解一下2023-12-12