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项目中怎么使用短信服务吧. 项目中的短信服务基本上上都会用到,简单的注册验证码,消息通知等等都会用到.所 ...
随机推荐
- 定位流之z-index属性
1.固定定位是脱离标准流的,不会占用标准流的空间 2.和绝对定位有点像,也不区分行内块级元素 3.类似于前面学的背景关联方式(让某个背景图片不随滚动而滚动)让某个元素不随着滚动条的滚动而滚动 ie6不 ...
- [Luogu P2014]选课 (树形DP)
题面 传送门:https://www.luogu.org/problemnew/show/P2014 Solution 这是一道十分经典的树形DP题,这种类型的树形DP有一种很普遍的解法. 首先,观察 ...
- 如何在Windows Server 2012及更高版本中将域控制器降级
如何在Windows Server 2012及更高版本中将域控制器降级 如果不降级就重装系统,会出问题,所以在将域控系统重装系统之前一定要先降级. 使用服务器管理器将 Windows Server 2 ...
- 庐山真面目之二微服务架构NGINX版本实现
一.简介 在上一篇文章<庐山真面目之微服务的简介和技术栈>中,我们已经探讨了微服务的来龙去脉,也说了想要实现微服务架构所需要的技术栈,今天我们开始实现一个微服务,当然这个 ...
- Spring Cloud Alibaba 之 user服务
项目技术选型 Spring Boot Spring MVC MyBatis + 通用Mapper (官网信息https://mapperhelper.github.io/docs/) Spring C ...
- C# 集合类(三)
C# 集合类自己经常用到: 数组(Array).动态数组(ArrayList).列表(List).哈希表(Hashtable).字典(Dictionary),对于经常使用的这些数据结构,做一个总结,便 ...
- Pytest学习(七) - skip、skipif的使用
前言 作为一个java党,我还是觉得pytest和testng很像,有时候真的会感觉到代码语言在某种程度上是相通的,那么今天来说说这两个知识点. skip和skipif,见名知意,就是跳过测试呗,直白 ...
- 它是世界上最好的语言,吊打PHP那种
Scratch Scratch是麻省理工媒体实验室终身幼稚园组开发的一套电脑程序开发平台,旨在让程序设计语言初学者不需先学习语言语法便能设计产品.开发者期望通过学习Scratch,启发和激励用户在愉快 ...
- Pandas_VBA_数据清洗对比
VBA处理数据与Python Pandas处理数据案例比较 Author : Collin_PXY 需求: 现有一个 csv文件,包含'CNUM'和'COMPANY'两列,数据里包含空行,且有内容重复 ...
- 获取元素计算样式getComputedStyle()与currentStyle
window.getComputedStyle()方法是标准化接口,返回一个对象,该对象在应用活动样式表并解析这些值可能包含的任何基本计算后报告元素的所有CSS属性的值. 私有的CSS属性值可以通过对 ...