當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot项目对接短信平台Demo
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot项目对接短信平台Demo
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、選擇短信平臺
我項目用的是云片短信平臺,官網(wǎng):https://www.yunpian.com/
注冊、登錄后,輸入個人或公司名稱,認證開發(fā)者信息(很簡單,需要身份證照片);完成后回到首頁,查看你的APIKEY(用以接口調(diào)用);第一步主要是獲取APIKEY!
二、加入Maven依賴
<!--短信平臺 --> <dependency><groupId>com.yunpian.sdk</groupId><artifactId>yunpian-java-sdk</artifactId><version>1.2.7</version> </dependency>三、寫短信工具類
package com.atage.utils.sms; /*** Created by luda on 19/01/08.*/ import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;/*** 短信http接口的java代碼調(diào)用示例* 基于Apache HttpClient 4.3** @author luda*/public class smsUtil {//查賬戶信息的http地址public static String APIKEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";public static String SIGN = "【云片網(wǎng)】";public static String TEMPLATE_CODE = "template_code";public static String TEMPLATE = "您的驗證碼是";//查賬戶信息的http地址private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json";//智能匹配模板發(fā)送接口的http地址private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";//模板發(fā)送接口的http地址private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";//發(fā)送語音驗證碼接口的http地址private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json";//綁定主叫、被叫關系的接口http地址private static String URI_SEND_BIND = "https://call.yunpian.com/v2/call/bind.json";//解綁主叫、被叫關系的接口http地址private static String URI_SEND_UNBIND = "https://call.yunpian.com/v2/call/unbind.json";//編碼格式。發(fā)送編碼格式統(tǒng)一用UTF-8private static String ENCODING = "UTF-8";public static void main(String[] args) throws IOException,URISyntaxException {//修改為您的apikey.apikey可在官網(wǎng)(http://www.yunpian.com)登錄后獲取String apikey = "48d**********************xxxxx";//修改為您要發(fā)送的手機號String mobile = "155******xx";/**************** 查賬戶信息調(diào)用示例 *****************/System.out.println(smsUtil.getUserInfo(apikey));/**************** 使用智能匹配模板接口發(fā)短信(推薦) *****************///設置您要發(fā)送的內(nèi)容(內(nèi)容必須和某個模板匹配。以下例子匹配的是系統(tǒng)提供的1號模板)String text = "【云片網(wǎng)】您的驗證碼是1234";//發(fā)短信調(diào)用示例System.out.println(smsUtil.sendSms(apikey, text, mobile));/**************** 使用指定模板接口發(fā)短信(不推薦,建議使用智能匹配模板接口) ******///設置模板ID,如使用1號模板:【#company#】您的驗證碼是#code# // long tpl_id = 1; // //設置對應的模板變量值 // // String tpl_value = URLEncoder.encode("#code#", ENCODING) + "=" + // URLEncoder.encode("1234", ENCODING) + "&" + URLEncoder.encode( // "#company#", ENCODING) + "=" + URLEncoder.encode("云片網(wǎng)", // ENCODING);//模板發(fā)送的調(diào)用示例 // System.out.println(tpl_value); // System.out.println(smsUtil.tplSendSms(apikey, tpl_id, tpl_value, // mobile));/**************** 使用接口發(fā)語音驗證碼 *****************/ // String code = "1234";//System.out.println(JavaSmsApi.sendVoice(apikey, mobile ,code));/**************** 使用接口綁定主被叫號碼 *****************///String from = "+86130xxxxxxxx";//String to = "+86131xxxxxxxx";//Integer duration = 30 * 60; // 綁定30分鐘//System.out.println(JavaSmsApi.bindCall(apikey, from ,to , duration));/**************** 使用接口解綁主被叫號碼 *****************///System.out.println(JavaSmsApi.unbindCall(apikey, from, to));}/*** 取賬戶信息** @return json格式字符串* @throws java.io.IOException*/public static String getUserInfo(String apikey) throws IOException,URISyntaxException {Map < String, String > params = new HashMap < String, String > ();params.put("apikey", apikey);return post(URI_GET_USER_INFO, params);}/*** 智能匹配模板接口發(fā)短信** @param apikey apikey* @param text 短信內(nèi)容* @param mobile 接受的手機號* @return json格式字符串* @throws IOException*/public static String sendSms(String apikey, String text,String mobile) throws IOException {Map < String, String > params = new HashMap < String, String > ();params.put("apikey", apikey);params.put("text", text);params.put("mobile", mobile);return post(URI_SEND_SMS, params);}/*** 通過模板發(fā)送短信(不推薦)** @param apikey apikey* @param tpl_id 模板id* @param tpl_value 模板變量值* @param mobile 接受的手機號* @return json格式字符串* @throws IOException*/public static String tplSendSms(String apikey, long tpl_id, String tpl_value,String mobile) throws IOException {Map < String, String > params = new HashMap < String, String > ();params.put("apikey", apikey);params.put("tpl_id", String.valueOf(tpl_id));params.put("tpl_value", tpl_value);params.put("mobile", mobile);return post(URI_TPL_SEND_SMS, params);}/*** 通過接口發(fā)送語音驗證碼* @param apikey apikey* @param mobile 接收的手機號* @param code 驗證碼* @return*/public static String sendVoice(String apikey, String mobile, String code) {Map < String, String > params = new HashMap < String, String > ();params.put("apikey", apikey);params.put("mobile", mobile);params.put("code", code);return post(URI_SEND_VOICE, params);}/*** 通過接口綁定主被叫號碼* @param apikey apikey* @param from 主叫* @param to 被叫* @param duration 有效時長,單位:秒* @returnpublic static String bindCall(String apikey, String from, String to,Integer duration) {Map < String, String > params = new HashMap < String, String > ();params.put("apikey", apikey);params.put("from", from);params.put("to", to);params.put("duration", String.valueOf(duration));return post(URI_SEND_BIND, params);}* 通過接口解綁綁定主被叫號碼* @param apikey apikey* @param from 主叫* @param to 被叫* @returnpublic static String unbindCall(String apikey, String from, String to) {Map < String, String > params = new HashMap < String, String > ();params.put("apikey", apikey);params.put("from", from);params.put("to", to);return post(URI_SEND_UNBIND, params);}*//*** 基于HttpClient 4.3的通用POST方法** @param url 提交的URL* @param paramsMap 提交<參數(shù),值>Map* @return 提交響應*/public static String post(String url, Map < String, String > paramsMap) {CloseableHttpClient client = HttpClients.createDefault();String responseText = "";CloseableHttpResponse response = null;try {HttpPost method = new HttpPost(url);if (paramsMap != null) {List < NameValuePair > paramList = new ArrayList <NameValuePair > ();for (Map.Entry < String, String > param: paramsMap.entrySet()) {NameValuePair pair = new BasicNameValuePair(param.getKey(),param.getValue());paramList.add(pair);}method.setEntity(new UrlEncodedFormEntity(paramList,ENCODING));}response = client.execute(method);HttpEntity entity = response.getEntity();if (entity != null) {responseText = EntityUtils.toString(entity, ENCODING);}} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (Exception e) {e.printStackTrace();}}return responseText;} }四、調(diào)用實例
//獲取四位隨機碼 Random random = new Random(); String smsCode=""; for (int i=0;i<4;i++){smsCode+=random.nextInt(10); } //將發(fā)送結果轉為json對象 JSONObject jsonObject = JSONObject.parseObject(smsUtil.sendSms(smsUtil.APIKEY,smsUtil.SIGN+smsUtil.TEMPLATE+smsCode,phone)); //將返回數(shù)據(jù)放入自己項目的返回實體中 if(Integer.parseInt(jsonObject.get("code").toString())==0){//狀態(tài)碼result.setType(TypeEnum.SUCCESS.getCode()); }else{result.setType(TypeEnum.FAIL.getCode()); } result.setMessage(jsonObject.get("msg").toString());//文字說明 if(result.getType().equals(TypeEnum.SUCCESS.getCode())||result.getType()==TypeEnum.SUCCESS.getCode()){result.setMessage("發(fā)送成功,60分鐘內(nèi)有效"); } //存如緩存 redis.setex(phone+smsUtil.TEMPLATE_CODE,smsCode,60*60);五、截圖
總結
以上是生活随笔為你收集整理的SpringBoot项目对接短信平台Demo的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C++深度剖析教程36】深入理解函数模
- 下一篇: java将xml中的标签名称转为小写_深