SpringBoot项目实现短信发送接口开发的实践

时间:2022-03-10 10:31:30

一. 短信接口实现

描述:请求第三方短信接口平台(而第三方短信平台的接口请求是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点击

SpringBoot项目实现短信发送接口开发的实践

③会出现以下界面,这里要注意 WSDL URL 地址要改为(短信平台请求接口地址,后边要加问号和wsdl ) 并也要更改Apache Axis 为 Glassfish /JAX-WS

SpringBoot项目实现短信发送接口开发的实践

④更改后出现此界面,点击OK后 , 会在新创建的包下出现idea自动创建的文件 .

SpringBoot项目实现短信发送接口开发的实践

⑤此时 把.class文件删除就好了,留下此三个文件(注意:SMSNewAllService类可以理解为打通webservice接口的实现类)

SpringBoot项目实现短信发送接口开发的实践

⑥业务实现及调用

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项目实现短信发送接口开发的文章就介绍到这了,更多相关SpringBoot项目实现短信发送接口开发内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_50831101/article/details/120861830