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
效果图
获取发送模板内容
/** * 获取发送短信内容,含发送内容 * @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"; }
赞 (0)