Maven+springboot+阿裡大於短信驗證服務
糾結點:Maven庫沒有sdk,需要解決
Maven打包找不到相關類,需要解決
ps:最近好久沒有寫點東西了,項目太緊,今天來一篇
在工作當中的項目中,我遇到過這種情況,公司架構選型SpringBoot ,這是個基於Maven構建的項目,
需要集成阿裡大魚短信系統,然後暴露REST風格的API接口出去。本文重點講解。
阿裡大於,是阿裡的通訊產品之一,提供短信驗證碼服務。
1、需要注冊阿裡大魚賬戶、充值、設置,這一塊不多講,官網有文檔。
(https://www.alidayu.com/)
2、下載官網開發包(SDK)

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
在官網就可以下載,下載完以後,解壓後我們看見:

我們接下來需要做的,就是將本地的這兩個文件,加入本地倉庫
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.xml中添加了
<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.xml.完整的代碼如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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 (一路上)