首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring Boot 整合阿里大於短信 SDK:Maven 本地 Jar 嵌入與打包解決方案

     編輯:Java 綜合教學

痛點

  1. 阿里大於 SDK 未上傳 Maven Central。
  2. 專案打包時找不到本地 Jar。

解決方案

1. 下載 SDK 並安裝到本地倉庫

# 解壓獲得 alidayu-sdk.jar、top-sdk.jar
mvn install:install-file \
  -DgroupId=com.taobao.sms1 -DartifactId=sms1 -Dversion=1.0.0 \
  -Dpackaging=jar -Dfile=/path/alidayu-sdk.jar

mvn install:install-file \
  -DgroupId=com.taobao.sms2 -DartifactId=sms2 -Dversion=1.0.0 \
  -Dpackaging=jar -Dfile=/path/top-sdk.jar

2. pom.xml 關鍵配置

<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>
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

<build>
    <plugins>
        <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>
        <!-- 解決打包找不到本地 jar -->
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArguments>
                    <extdirs>src/main/webapp/WEB-INF/lib</extdirs>
                </compilerArguments>
            </configuration>
        </plugin>
    </plugins>
</build>

3. 核心服務實現

@Service
public class DaYuService {
    public Result sendSmsCode(String phone) {
        String code = RandomUtil.randomCode(6);
        TaobaoClient client = new DefaultTaobaoClient(
            "https://gw.api.taobao.com/router/rest", "appkey", "secret");
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setSmsType("normal");
        req.setSmsFreeSignName("我的應用");
        req.setRecNum(phone);
        req.setSmsTemplateCode("SMS_8926569");
        req.setSmsParamString("{\"code\":\"" + code + "\"}");
        try {
            client.execute(req);
            smsCache.save(phone, code);  // 自行實現驗證碼存儲
            return Result.ok();
        } catch (ApiException e) {
            return Result.fail("發送失敗");
        }
    }
}

關鍵提示

  • groupId/artifactId 自定義即可,但需與 install:install-file 參數一致。
  • maven-compiler-plugin 的 extdirs 指向存放 jar 的目錄,確保打包時編譯器能找到。
  • 生產環境建議改用阿里云短信服務新版 SDK(已上 Maven Central)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved