您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot項(xiàng)目之怎么實(shí)現(xiàn)短信發(fā)送接口”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringBoot項(xiàng)目之怎么實(shí)現(xiàn)短信發(fā)送接口”吧!
描述:請(qǐng)求第三方短信接口平臺(tái)(而第三方短信平臺(tái)的接口請(qǐng)求是webservice方式實(shí)現(xiàn)的),此時(shí)我們要測(cè)試接口是否通,要用的工具SoapUI測(cè)試工具, 不能用PostMan,即使用post組裝完參數(shù)請(qǐng)求該短信平臺(tái)接口也不會(huì)通的(請(qǐng)求之前要ping通IP,只有在同一網(wǎng)段才可請(qǐng)求.或者使用VPN遠(yuǎn)程連接也可請(qǐng)求),接口通了之后.開始裸代碼.代碼使用IDEA工具去完成 , 實(shí)現(xiàn)邏輯根據(jù)需求而定.
<!--生成短信代碼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>
①創(chuàng)建一個(gè)新的包,鼠標(biāo)點(diǎn)擊此包
②找到generate點(diǎn)擊
③會(huì)出現(xiàn)以下界面,這里要注意 WSDL URL 地址要改為(短信平臺(tái)請(qǐng)求接口地址,后邊要加問號(hào)和wsdl ) 并也要更改Apache Axis 為 Glassfish /JAX-WS
④更改后出現(xiàn)此界面,點(diǎn)擊OK后 , 會(huì)在新創(chuàng)建的包下出現(xiàn)idea自動(dòng)創(chuàng)建的文件 .
⑤此時(shí) 把.class文件刪除就好了,留下此三個(gè)文件(注意:SMSNewAllService類可以理解為打通webservice接口的實(shí)現(xiàn)類)
⑥業(yè)務(wù)實(shí)現(xiàn)及調(diào)用
package com.enn.claim.config.sms; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 獲取配置文件中: 發(fā)送短信平臺(tái)地址/賬號(hào)/密碼/短信類型/費(fèi)用部門等 參數(shù) */ @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 + '\'' + '}'; } }
/** * 發(fā)送短信 * @param sendMessageVo * @return */ @Override public SendSmsResult sendMessage(SendSmsVo sendMessageVo) { // 0.參數(shù)校驗(yàn) sendMessageVo = this.validParams(sendMessageVo); // 1.創(chuàng)建返回結(jié)果集 SendSmsResult smsResult = new SendSmsResult(); try { //2.拼裝發(fā)送手機(jī)號(hào)碼 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.發(fā)送短信 log.info("開始請(qǐng)求發(fā)送短信>>>>>>>>>>>>>>>>>>"+"開始發(fā)送短信"); 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("請(qǐng)求發(fā)送短信返回值>>>>>>>>>>>>>>>>>>{}: "+resultSend); //4.判斷返回結(jié)果 if (ObjectUtils.isEmpty(resultSend)) { return SmsResultUtil.error("請(qǐng)求發(fā)送短信失敗,短信服務(wù)商無響應(yīng)!"); } //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, "請(qǐng)求發(fā)送短信失敗,服務(wù)商返回錯(cuò)誤信息為:" + errorMsg, ""); return smsResult; } }catch (Exception e){ log.info("請(qǐng)求發(fā)送短信失敗,系統(tǒng)異常{}", e); e.printStackTrace(); return SmsResultUtil.error("請(qǐng)求發(fā)送短信失敗,系統(tǒng)異常!"); }finally { log.info("發(fā)送短信結(jié)果{}", JSONObject.toJSONString(smsResult)); } }
⑦yml配置文件配置
感謝各位的閱讀,以上就是“SpringBoot項(xiàng)目之怎么實(shí)現(xiàn)短信發(fā)送接口”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)SpringBoot項(xiàng)目之怎么實(shí)現(xiàn)短信發(fā)送接口這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。