SpringBoot整合阿里短信服務


導讀

  由於最近手頭上需要做個Message Gateway,涉及到:郵件(點我直達)、短信、公眾號(點我直達)等推送功能,網上學習下,整理下來以備以后使用。

步驟

  點我直達

登錄短信服務控制台

  點我直達

開通短信服務

快速學習

測試短信發送

發送短息

  報一下錯誤信息

抱歉!發送出錯了。錯誤碼Code:isv.AMOUNT_NOT_ENOUGH。建議前往“短信接口調用錯誤碼”幫助文檔,根據錯誤碼查詢錯誤原因及建議。

查看錯誤碼顯示,提示余額不足,先充點錢進去

  賬戶里充點錢進去,我充了3元

再次發送消息

收到的短信測試消息

查看demo

查看sdk

添加依賴即java代碼示例

注意事項

  簽名和短信模板自己添加!!!!!點我直達

具體請求參數,請查閱API文檔

創建AccessKey和AccessSercet

  相當於用戶的身份標識,項目中需要用到~

SpringBoot代碼實現

添加依賴

     <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.6</version>
        </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.11.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>springboot-sms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-sms</name>
    <description>SpringBoot整合阿里短信服務</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--阿里短信服務-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
pom.xml

控制層

package com.ybchen.springbootsms.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName:SmsController
 * @Description:TODO
 * @Author:chenyb
 * @Date:2020/11/30 10:59 上午
 * @Versiion:1.0
 */
@RestController
public class SmsController {
    @GetMapping("sendSms")
    public Object sendSms(){
        //區域id、accessKeyId、secret
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GDpBasmRFrABc8oNLNm", "FZ8hqtLe8xeh1Nb285olKBL5lBiX9F");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //區域id
        request.putQueryParameter("RegionId", "cn-hangzhou");
        //發送的手機號
        request.putQueryParameter("PhoneNumbers", "199500000000");
        //簽名
        request.putQueryParameter("SignName", "ABC商城");
        //模板
        request.putQueryParameter("TemplateCode", "SMS_205887565");
        //數據,json格式替換短信模板的內容
        request.putQueryParameter("TemplateParam", "{\"code\":\"9999\"}");
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }
}

發送短信

錯誤碼文檔

  點我直達

APIdemo調試

  點我直達

接收短信回復消息內容

方式一

參考文檔

點我直達

點我直達

阿里雲MNS

點我直達

項目下載

鏈接: https://pan.baidu.com/s/1LgY01b_yU2n0hIDrmS7akQ  密碼: jcs6

效果圖

 

方式二

  登錄阿里雲后台管理頁面,設置Http批量推送消息即可,如:http://xxx.xxx.xxx.xxx:9999/handler

獲取發送模板內容

    /**
     * 獲取發送短信內容,含發送內容
     * @return
     */
    @GetMapping("getDetail")
    public Object getDetail(){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "xxx", "yyy");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("QuerySendDetails");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumber", "199xxxxx");
        request.putQueryParameter("SendDate", "20210204");
        //簽名
        request.putQueryParameter("SignName", "簽名");
        //模板
        request.putQueryParameter("TemplateCode", "模板編號");

        request.putQueryParameter("PageSize", "10");
        request.putQueryParameter("CurrentPage", "1");
        request.putQueryParameter("BizId", "959409912404993912^0");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "ok";
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM