星空网 > 软件开发 > Java

【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

Maven+springboot+阿里大于短信验证服务

纠结点:Maven库没有sdk,需要解决

    Maven打包找不到相关类,需要解决

ps:最近好久没有写点东西了,项目太紧,今天来一篇

一、本文简介

  在工作当中的项目中,我遇到过这种情况,公司架构选型SpringBoot ,这是个基于Maven构建的项目,

  需要集成阿里大鱼短信系统,然后暴露REST风格的API接口出去。本文重点讲解。

二、开发准备

  阿里大于,是阿里的通讯产品之一,提供短信验证码服务。

  1、需要注册阿里大鱼账户、充值、设置,这一块不多讲,官网有文档。

    (https://www.alidayu.com/)

  2、下载官网开发包(SDK)

    【微框架】Maven +SpringBoot  集成 阿里大鱼 短信接口详解与Demo

  3、官方给的demo例子(先参考下,主要看我的Demo)

TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();req.setExtend("123456");req.setSmsType("normal");req.setSmsFreeSignName("阿里大于");req.setSmsParamString("{\"code\":\"1234\",\"product\":\"alidayu\"}");req.setRecNum("13000000000");req.setSmsTemplateCode("SMS_585014");AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);System.out.println(rsp.getBody());

 

三、正式开发

  部分同学可能使用的框架不是springboot ,没关系,举一反三,这个不影响,主要看实现。

  1、创建Maven项目

    不多讲!

 

  2、阿里大鱼SDK工具包配置

    阿里大鱼的sdk在maven库中,无法找到,所以,我们要加入本地的jar.这一步骤,我们需要完成该配置

    2.1、下载阿里大鱼SDK

       在官网就可以下载,下载完以后,解压后我们看见:

        【微框架】Maven +SpringBoot  集成 阿里大鱼 短信接口详解与Demo

        我们接下来需要做的,就是将本地的这两个文件,加入本地仓库

    2.2、将SDK加入本地Maven仓库

      命令行讲解如下:

      (groupid,artifactId,version都是随便填)

      mvn install:install-file 
          -DgroupId=FCKeditor 
          -DartifactId=FCKeditor     
          -Dversion=2.3         //版本号
          -Dpackaging=jar               //类型
          -Dfile=d:\FCKeditor-2.3.jar     //jar实际路径

        
      相当与在pom. 
        <dependency> 
          <groupId>FCKeditor</groupId> 
          <artifactId>FCKeditor 
          </artifactId> 
          <version>2.3</version> 
        </dependency> 

      运行命令行(按照自己实际情况修改,都在一行上面):

     

 mvn install:install-file:-DgroupId=FCKeditor -DartifactId=FCKeditor -Dversion=2.3 -Dpackaging=jar -Dfile=d:\FCKeditor-2.3.jar

      完了你就可以看到,你的maven库中会有你刚刚添加的包 了

     2.3、需要在maven中加入配置插件

        
       这一步很重要,要不然,打包的时候无法找到本地jar.

    

<!-- 解决本地jar植入的插件 -->      <plugin>        <artifactId>maven-compiler-plugin</artifactId>        <configuration>          <source>1.6</source>          <target>1.6</target>          <encoding>UTF-8</encoding>          <compilerArguments>            <extdirs>src\main\webapp\WEB-INF\lib</extdirs>          </compilerArguments>        </configuration>      </plugin>

 

  3、加入SpringBoot需要的依赖

    springboot pom.

    

<project ="http://maven.apache.org/POM/4.0.0" ="http://www.w3.org/2001/  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>cc-hyh-demo</groupId>  <artifactId>cc.hyh.demo</artifactId>  <version>0.0.1-SNAPSHOT</version>  <!-- parent依赖 -->  <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.3.6.RELEASE</version>  </parent>  <build>    <sourceDirectory>src/main/java</sourceDirectory>    <plugins>      <!-- 打包插件 -->      <plugin>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-maven-plugin</artifactId>        <executions>          <execution>            <goals>              <goal>repackage</goal>            </goals>          </execution>        </executions>        <configuration>          <source>1.6</source>          <target>1.6</target>          <encoding>UTF-8</encoding>        </configuration>      </plugin>      <!-- 解决本地jar植入的插件 -->      <plugin>        <artifactId>maven-compiler-plugin</artifactId>        <configuration>          <source>1.6</source>          <target>1.6</target>          <encoding>UTF-8</encoding>          <compilerArguments>            <extdirs>src\main\webapp\WEB-INF\lib</extdirs>          </compilerArguments>        </configuration>      </plugin>    </plugins>  </build>  <dependencies>    <!-- 阿里云 短信 依赖 -->    <dependency>      <groupId>com.taobao.sms1</groupId>      <artifactId>sms1</artifactId>      <version>1.0.0</version>    </dependency>    <dependency>      <groupId>com.taobao.sms2</groupId>      <artifactId>sms2</artifactId>      <version>1.0.0</version>    </dependency>    <!-- springboot 依赖 -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-tomcat</artifactId>      <scope>provided</scope>    </dependency>  </dependencies></project>

  注意:阿里短信的包,是我自己做到本地库中的,不要照搬。

  4、主要的服务类

  其他次要的代码省略。主要实现发送验证码代码:

  

package cc.dayu.service;import java.util.Date;public class DaYuService {    public Info requestSmsCode(String phone) {
     //这个只是我项目中的标识,忽略 Info in = new Info();
     //阿里大鱼URL String url = "http://gw.api.taobao.com/router/rest";
     //我自己随机生成了六位数验证码,自己去实现 String code = RandomUtil.createRandomVcode();
     //以下才是重点 三个参数,一个url阿里大鱼的服务地址,其他两个去阿里大鱼后端查看自己的相应的参数 TaobaoClient client = new DefaultTaobaoClient(url, "23334143", "23gyfruy38hf83yh7y8u98j9u9j9i9"); // String json="{\"code\":\"1234\",\"product\":\"某某电子商务验证\"}"; AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest(); req.setExtend("1"); //必须填写normal
     req.setSmsType("normal");
     //你应用的名字 req.setSmsFreeSignName("XXXX");    //电话号码 req.setRecNum(phone);
    //模板 req.setSmsTemplateCode("SMS_8926569");
    //模板中的参数,按照实际情况去 req.setSmsParamString("{msg:'" + code + "'}"); try { AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req); // 这里是我设置的一个保存验证码 机制。按照实际需求,自行设计 UserSms userSms = new UserSms(); userSms.setPhone(phone); userSms.setCode(code); userSms.setTime(new Date()); sms.addSms(userSms); in.setStatus("发送成功"); } catch (ApiException e) { // TODO Auto-generated catch block // e.printStackTrace(); in.setStatus("发送失败"); } return in; }}

四、交流方式

  其实对于Maven项目添加阿里大鱼这样的Demo,主要的问题在于,如何将阿里大鱼jar加入maven,并且能打包成功,

  需要源码或交流沟通,请加WX:  wixf150  (一路上)

 




原标题:【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

沃尔玛出售日本西友超市85%股权给KKR、乐天:https://www.ikjzd.com/articles/133997
Amazon多变体的玩法:https://www.ikjzd.com/articles/133998
亚马逊中国向贫困地区学生捐赠“温暖爱心包”:https://www.ikjzd.com/articles/133999
如何避免亚马逊被封号、被关联的风险?:https://www.ikjzd.com/articles/134
亚马逊海外购物节推出史上最长“真黑五”全球狂欢:https://www.ikjzd.com/articles/134000
亚马逊a+页面是什么?如何设计?有什么需要注意的吗?:https://www.ikjzd.com/articles/134001
跨境支付百科——巴西支付篇:https://www.kjdsnews.com/a/1836648.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
相关文章
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流