SpringBoot項目實現短信發送接口開發的實踐
一. 短信接口實現
描述:請求第三方短信接口平臺(而第三方短信平臺的接口請求是webservice方式實現的),此時我們要測試接口是否通,要用的工具SoapUI測試工具, 不能用PostMan,即使用post組裝完參數請求該短信平臺接口也不會通的(請求之前要ping通IP,隻有在同一網段才可請求.或者使用VPN遠程連接也可請求),接口通瞭之後.開始裸代碼.代碼使用IDEA工具去完成 , 實現邏輯根據需求而定.
首先導入兩個依賴
<!--生成短信代碼webservice START--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency>
webservice方式實現(IDEA)
①創建一個新的包,鼠標點擊此包
②找到generate點擊
③會出現以下界面,這裡要註意 WSDL URL 地址要改為(短信平臺請求接口地址,後邊要加問號和wsdl ) 並也要更改Apache Axis 為 Glassfish /JAX-WS
④更改後出現此界面,點擊OK後 , 會在新創建的包下出現idea自動創建的文件 .
⑤此時 把.class文件刪除就好瞭,留下此三個文件(註意:SMSNewAllService類可以理解為打通webservice接口的實現類)
⑥業務實現及調用
package com.enn.claim.config.sms; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 獲取配置文件中: 發送短信平臺地址/賬號/密碼/短信類型/費用部門等 參數 */ @Component @ConfigurationProperties(prefix = "send-messages") public class SmsProperties { private String address; private String userName; private String passWord; private String smsType; private String feeDept; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return passWord; } public void setPassword(String password) { this.passWord = password; } public String getSmsType() { return smsType; } public void setSmsType(String smsType) { this.smsType = smsType; } public String getFeeDept() { return feeDept; } public void setFeeDept(String feeDept) { this.feeDept = feeDept; } @Override public String toString() { return "SmsProperties{" + "address='" + address + '\'' + ", userName='" + userName + '\'' + ", passWord='" + passWord + '\'' + ", smsType='" + smsType + '\'' + ", feeDept='" + feeDept + '\'' + '}'; } }
/** * 發送短信 * @param sendMessageVo * @return */ @Override public SendSmsResult sendMessage(SendSmsVo sendMessageVo) { // 0.參數校驗 sendMessageVo = this.validParams(sendMessageVo); // 1.創建返回結果集 SendSmsResult smsResult = new SendSmsResult(); try { //2.拼裝發送手機號碼 String mobiles = ""; for (int i = 0; i < sendMessageVo.getMobile().size(); i++) { if (i == 0) { mobiles = sendMessageVo.getMobile().get(i); } else { mobiles = mobiles + "," + sendMessageVo.getMobile().get(i); } } //3.發送短信 log.info("開始請求發送短信>>>>>>>>>>>>>>>>>>"+"開始發送短信"); SMSNewAllService service = new SMSNewAllService(); ISMSNewAll ismsNewAll = service.getISMSNewAllPort(); String resultSend = ismsNewAll.sendSMSWithExt(smsProperties.getUserName(), smsProperties.getPassword(), mobiles, sendMessageVo.getSmsBody(), smsProperties.getSmsType(), sendMessageVo.getReplyFlag(), smsProperties.getFeeDept(), sendMessageVo.getAllowStartTime(), sendMessageVo.getAllowEndTime(), sendMessageVo.getDeadLineTime(), sendMessageVo.getAppointTime(), sendMessageVo.getArea(), sendMessageVo.getExtCode()); log.info("請求發送短信返回值>>>>>>>>>>>>>>>>>>{}: "+resultSend); //4.判斷返回結果 if (ObjectUtils.isEmpty(resultSend)) { return SmsResultUtil.error("請求發送短信失敗,短信服務商無響應!"); } //5.處理返回信息 JSONObject jsonObject = JSONObject.parseObject(resultSend); String errorCode = jsonObject.get("errorCode").toString(); String status = jsonObject.get("status").toString(); if ("0".equals(errorCode) && "ok".equals(status)) { String msgInfo = jsonObject.getString("msgInfo"); smsResult = SmsResultUtil.returnMessage(SmsConstants.SystemCode.OK, SmsConstants.MessageCode.SUCCESS_CODE, SmsConstants.MessageMsg.SUCCESS_MSG, msgInfo); return smsResult; } else { String errorMsg = jsonObject.getString("errorMsg"); smsResult = SmsResultUtil.returnMessage(SmsConstants.SystemCode.ERROR, SmsConstants.MessageCode.FAIL_CODE, "請求發送短信失敗,服務商返回錯誤信息為:" + errorMsg, ""); return smsResult; } }catch (Exception e){ log.info("請求發送短信失敗,系統異常{}", e); e.printStackTrace(); return SmsResultUtil.error("請求發送短信失敗,系統異常!"); }finally { log.info("發送短信結果{}", JSONObject.toJSONString(smsResult)); } }
⑦yml配置文件配置
到此這篇關於SpringBoot項目實現短信發送接口開發的文章就介紹到這瞭,更多相關SpringBoot項目實現短信發送接口開發內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 詳解Java基礎之封裝
- Springboot 讀取自定義pro文件註入static靜態變量方式
- Spring原生Rpc六種的正確打開方式實現示例
- SpringMvc接受請求參數的幾種情況演示
- Java實現用戶管理系統