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调试
SpringBoot整合阿里短信服务的更多相关文章
- PHP接入阿里云市场 阿里短信服务DEMO
阿里短信服务:支持三大运营商短信.智能管道等优质能力,产品特点:3秒可达.99%到达率.超低资费. 使用阿里短信服务步骤: 1.购买服务 到https://market.aliyun.com/prod ...
- PHP开发实用-阿里短信服务(Short Message Service)
步骤 1 使用阿里云短信服务正常发短信需要 短信签名 短信模板 1申请短信签名 根据用户属性来创建符合自身属性的签名信息.企业用户需要上传相关企业资质证明,个人用户需要上传证明个人身份的证明. ...
- 阿里云短信服务(JAVA)
一,前言 短信验证码想必大家都不陌生,在很多网站,APP中都有使用到.比如登录,注册,身份校验等场景.不过通常情况下,短信服务都是外包给第三方公司的,接下来向大家分享如何使用阿里的短信服务. 二, ...
- python3配置阿里云短信服务
1.申请阿里云短信服务,具体步骤看我的python2-Django配置短信服务 2.安装依赖 aliyun-python-sdk-core-v3 aliyun-python-sdk-dysmsapi= ...
- springboot 使用阿里云短信服务发送验证码
一.申请阿里云短信服务 1.申请签名 2.申请模板 3.创建accesskey(鼠标悬停在右上角头像) 二.代码实现 1.springboot引入maven依赖 <dependency> ...
- springboot 项目使用阿里云短信服务发送手机验证码
1.注册阿里云账户进行账号实名认证 2.申请短信签名和模板 3.创建access_key和access_secret 4.然后就是代码编写 一.找到产品与服务里面的云通信模块,然后找到短信服务,开通短 ...
- 阿里云短信服务bug
接入阿里云短信服务,在springboot中写测试方法,执行到 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou ...
- java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况
java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况 邮件短信微服务 spring boot 微服务 接收json格式参数 验证参数合 ...
- 六、springboot 简单优雅是实现短信服务
前言 上一篇讲了 springboot 集成邮件服务,接下来让我们一起学习下springboot项目中怎么使用短信服务吧. 项目中的短信服务基本上上都会用到,简单的注册验证码,消息通知等等都会用到.所 ...
随机推荐
- 【QT】 QThread部分源码浅析
本文章挑出QThread源码中部分重点代码来说明QThread启动到结束的过程是怎么调度的.其次因为到了Qt4.4版本,Qt的多线程就有所变化,所以本章会以Qt4.0.1和Qt5.6.2版本的源码来进 ...
- 【Luogu】P1402 酒店之王 题解
原题链接 这道题,很明显是个配对问题.于是,我们可以想到用网络最大流来做. 先整理一下题目条件. 很明显,根据贪心思想,要使最多人满意,每个人应该最多睡一个房间(似乎也没有人能睡两个房间),吃一道菜. ...
- 1_Two Sum
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- 阿里巴巴开发手册强制使用SLF4J作为门面担当的秘密,我搞清楚了
之前已经详细.全面地介绍了 Log4j,相信小伙伴们已经完全掌握了.那我在读嵩山版的阿里巴巴开发手册(没有的小伙伴,记着找我要)的时候,就发现了一条「强制」性质的日志规约: 应用中不可以直接使用日志系 ...
- 【QT】跨线程的信号槽(connect函数)
线程的信号槽机制需要开启线程的事件循环机制,即调用QThread::exec()函数开启线程的事件循环. Qt信号-槽连接函数原型如下: bool QObject::connect ( const Q ...
- leetcode7:binary-tree-preorder-traversal
题目描述 求给定的二叉树的前序遍历. 例如: 给定的二叉树为{1,#,2,3}, 1 \ 2 / 3 返回:[1,2,3]. 备注:用递归来解这道题太没有新意了,可以给出迭代的解法么? ...
- day86:luffy:前端发送请求生成订单&结算页面优惠劵的实现
目录 1.前端发送请求生成订单 1.前端点击支付按钮生成订单 2.结算成功之后应该清除结算页面的数据 3.后端计算结算页面总原价格和总的真实价格并存到数据库订单表中 2.优惠劵 1.准备工作 2.前端 ...
- CSS选择器组合符号
组合连接符号 无连接符 多项条件 空格符 后代节点 > 子节点 + 兄弟节点 ~ 兄弟节点 这里要注意+和~之间的区别 +是后面紧邻兄弟 ~是后面所有兄弟
- 关于情感分类(Sentiment Classification)的文献整理
最近对NLP中情感分类子方向的研究有些兴趣,在此整理下个人阅读的笔记(持续更新中): 1. Thumbs up? Sentiment classification using machine lear ...
- 前端动画框架GSAP框架随笔
gsap是目前非常流行的前端动画框架,可以非常轻松构造出复杂的动画效果,这里仅对我实际使用中的一些例子进行总结 官网 示例 文章种所使用代码的在线示例 基础用法 // 声明一个滚动控制器 let ct ...