Skip to content

Commit b9371f2

Browse files
committed
ApiBoot Sms示例添加
1 parent cc04512 commit b9371f2

File tree

7 files changed

+209
-1
lines changed

7 files changed

+209
-1
lines changed

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/sms/ApiBootSmsProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class ApiBootSmsProperties {
4646
*/
4747
private long readTimeout = 10000;
4848
/**
49-
* 短信环境
49+
* 短信区域环境
5050
*/
5151
private String profile = "default";
5252
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## ApiBoot Alibaba Sms
2+
3+
`ApiBoot`的短信服务模块是由`阿里云`的国际短信服务提供的,支持国内和国际快速发送验证码、短信通知和推广短信。
4+
5+
> 前提:需要到阿里云控制台申请开通短信服务。
6+
7+
### 引入ApiBoot Alibaba Sms
8+
9+
`pom.xml`配置文件内添加如下:
10+
11+
```xml
12+
<!--ApiBoot Alibaba Sms-->
13+
<dependency>
14+
<groupId>org.minbox.framework</groupId>
15+
<artifactId>api-boot-starter-alibaba-sms</artifactId>
16+
</dependency>
17+
```
18+
19+
`ApiBoot`所提供的依赖都不需要添加版本号,具体查看[ApiBoot版本依赖](https://github.com/hengboy/api-boot/blob/master/README.md#%E6%B7%BB%E5%8A%A0%E7%89%88%E6%9C%AC%E4%BE%9D%E8%B5%96)
20+
21+
### 配置参数列表
22+
23+
| 配置参数 | 参数介绍 | 默认值 | 是否必填 |
24+
| --------------------------------- | ------------------------ | ------- | -------- |
25+
| `api.boot.sms.access-key-id` | RAM账号的AccessKey ID |||
26+
| `api.boot.sms.access-key-secret` | RAM账号Access Key Secret |||
27+
| `api.boot.sms.sign-name` | 短信签名 |||
28+
| `api.boot.sms.connection-timeout` | 短信发送连接超时时长 | 10000 ||
29+
| `api.boot.sms.read-timeout` | 短信接收消息连接超时时长 | 10000 ||
30+
| `api.boot.sms.profile` | 短信区域环境 | default ||
31+
32+
### 发送短信
33+
34+
`ApiBoot Alibaba Sms`模块内置了`ApiBootSmsService`接口实现类,通过`send`方法即可完成短信发送,如下所示:
35+
36+
```java
37+
/**
38+
* logger instance
39+
*/
40+
static Logger logger = LoggerFactory.getLogger(ApiBootSmsTest.class);
41+
42+
@Autowired
43+
private ApiBootSmsService apiBootSmsService;
44+
45+
@Test
46+
public void sendSms() {
47+
48+
// 参数
49+
ApiBootSmsRequestParam param = new ApiBootSmsRequestParam();
50+
param.put("code", "192369");
51+
52+
// 请求对象
53+
ApiBootSmsRequest request = ApiBootSmsRequest.builder().phone("171xxxxx").templateCode("SMS_150761253").param(param).build();
54+
55+
// 发送短信
56+
ApiBootSmsResponse response = apiBootSmsService.send(request);
57+
logger.info("短信发送反馈,是否成功:{}", response.isSuccess());
58+
}
59+
```
60+
61+
> 短信模板code自行从阿里云控制台获取。
62+
63+
如果在阿里云控制台定义的短信模板存在多个参数,可以通过`ApiBootSmsRequestParam#put`方法来进行挨个添加,该方法返回值为`ApiBootSmsRequestParam`本对象。
64+
65+
#### 多参数
66+
67+
多参数调用如下所示:
68+
69+
```java
70+
// 参数
71+
ApiBootSmsRequestParam param = new ApiBootSmsRequestParam();
72+
param.put("code", "192369").put("name", "测试名称");
73+
```
74+
75+
#### 发送结果反馈
76+
77+
执行短信发送后会返回`ApiBootSmsResponse`实例,通过该实例即可判断短信是否发送成功。
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>api-boot-samples</artifactId>
7+
<groupId>org.minbox.framework</groupId>
8+
<version>2.1.1.RELEASE</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<description>ApiBoot集成阿里云国际短信服务.</description>
12+
<artifactId>api-boot-sample-alibaba-sms</artifactId>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter</artifactId>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-test</artifactId>
21+
</dependency>
22+
<!--ApiBoot Alibaba Sms-->
23+
<dependency>
24+
<groupId>org.minbox.framework</groupId>
25+
<artifactId>api-boot-starter-alibaba-sms</artifactId>
26+
</dependency>
27+
</dependencies>
28+
<dependencyManagement>
29+
<dependencies>
30+
<!--springboot dependencies-->
31+
<dependency>
32+
<groupId>org.minbox.framework</groupId>
33+
<artifactId>api-boot-dependencies</artifactId>
34+
<version>2.1.1.RELEASE</version>
35+
<type>pom</type>
36+
<scope>import</scope>
37+
</dependency>
38+
</dependencies>
39+
</dependencyManagement>
40+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.minbox.framework.api.boot.sample.sms;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
/**
9+
* @author:恒宇少年 - 于起宇
10+
* <p>
11+
* DateTime:2019-03-22 13:48
12+
* Blog:http://blog.yuqiyu.com
13+
* WebSite:http://www.jianshu.com/u/092df3f77bca
14+
* Gitee:https://gitee.com/hengboy
15+
* GitHub:https://github.com/hengyuboy
16+
*/
17+
@SpringBootApplication
18+
public class ApiBootSmsSampleApplication {
19+
/**
20+
* logger instance
21+
*/
22+
static Logger logger = LoggerFactory.getLogger(ApiBootSmsSampleApplication.class);
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(ApiBootSmsSampleApplication.class, args);
26+
logger.info("「「「「「ApiBoot Sms 示例启动成功.」」」」」");
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring:
2+
application:
3+
name: api-boot-sample-alibaba-sms
4+
# ApiBoot Sms
5+
api:
6+
boot:
7+
sms:
8+
access-key-id: xxxx
9+
access-key-secret: xxx
10+
sign-name: 签名
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.minbox.framework.api.boot.sample.sms;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.minbox.framework.api.boot.plugin.sms.ApiBootSmsService;
6+
import org.minbox.framework.api.boot.plugin.sms.request.ApiBootSmsRequest;
7+
import org.minbox.framework.api.boot.plugin.sms.request.ApiBootSmsRequestParam;
8+
import org.minbox.framework.api.boot.plugin.sms.response.ApiBootSmsResponse;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
/**
16+
* ApiBoot Sms 单元测试
17+
*
18+
* @author:恒宇少年 - 于起宇
19+
* <p>
20+
* DateTime:2019-03-22 13:49
21+
* Blog:http://blog.yuqiyu.com
22+
* WebSite:http://www.jianshu.com/u/092df3f77bca
23+
* Gitee:https://gitee.com/hengboy
24+
* GitHub:https://github.com/hengyuboy
25+
*/
26+
@RunWith(SpringRunner.class)
27+
@SpringBootTest(classes = ApiBootSmsSampleApplication.class)
28+
public class ApiBootSmsTest {
29+
/**
30+
* logger instance
31+
*/
32+
static Logger logger = LoggerFactory.getLogger(ApiBootSmsTest.class);
33+
34+
@Autowired
35+
private ApiBootSmsService apiBootSmsService;
36+
37+
@Test
38+
public void sendSms() {
39+
40+
// 参数
41+
ApiBootSmsRequestParam param = new ApiBootSmsRequestParam();
42+
param.put("code", "192369").put("name", "测试名称");
43+
44+
45+
// 请求对象
46+
ApiBootSmsRequest request = ApiBootSmsRequest.builder().phone("171xxxxx").templateCode("SMS_150761253").param(param).build();
47+
48+
// 发送短信
49+
ApiBootSmsResponse response = apiBootSmsService.send(request);
50+
logger.info("短信发送反馈,是否成功:{}", response.isSuccess());
51+
}
52+
}

api-boot-samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>api-boot-sample-security-oauth-jwt</module>
1717
<module>api-boot-sample-swagger</module>
1818
<module>api-boot-sample-alibaba-oss</module>
19+
<module>api-boot-sample-alibaba-sms</module>
1920
</modules>
2021

2122
<build>

0 commit comments

Comments
 (0)