A simple and approachable SMS service Spring Boot Starter

Foreword

SMS services in user registration, login, password recovery and other related operations, allowing users to use more convenient, more and more companies are using SMS verification mode allows users to operate, thereby improving the user's usability.

Spring Boot Starter

Since the concept of greater than convention Spring Boot arranged such that in use Spring easier. Spring Boot project offers a lot of Starter, we make it very easy when using Spring. Starter for official adoption at the beginning of spring-boot-starter-xxx, for Spring Boot Starter unofficial offer, the official recommended xxxx-spring-boot-starter named.

Short Message Service Starter

1. compiler and development tools

IntelliJ IDEA 2018.2.5
Maven 3.5
JDK 1.8

2. How to use sms-spring-boot-starter

(1). Pom document incorporated

   <dependency>
         <groupId>com.github.jackieonway.sms</groupId>
         <artifactId>sms-spring-boot-starter</artifactId>
         <version>0.0.1-SNAPSHOT</version>
     </dependency>

Snapshots address configuration maven central repository in pom.xml

<repositories>
      <repository>
          <id>mavenRepoCenter</id>
          <name>Maven Development Snapshot Repository</name>
          <url>https://oss.sonatype.org/content/repositories/snapshots</url>
          <releases>
              <enabled>false</enabled>
          </releases>
          <snapshots>
              <enabled>true</enabled>
          </snapshots>
      </repository>
  </repositories>

(2) added in the application.yml

spring:
 pengzu:
   sms:
     sms-type: tentcent  # 短信服务商 暂目前只有 腾讯和阿里的短信服务
     security-key: your security-key # 短信的私钥
     appid: your appid # 短信的应用id
     sign: your sign # 短信的签名

(3) added in the main program Springboot

@EnabledPengzuSmsAutoConfiguration
@ComponentScan(basePackages = {"com.example.demo.sms", //项目包
"com.pengzu.sms"}) // 短信服务所在包

(4) Create a program to send text messages

@RestController
public class HelloController {

    @Autowired
    private PengzuSmsService pengzuSmsService;

    @GetMapping("/sayHello")
    public Object sayHello() throws ClientException, HTTPException, IOException {
        // 具体参数请参考各短信服务商
        // your template params
        String[] paramst = {"5678","5"};
        TencentSmsRequest tencentSmsRequest = new TencentSmsRequest();
        tencentSmsRequest.setPhoneNumber(new String[]{"your cellphone"});
        tencentSmsRequest.setParams(paramst);
        return pengzuSmsService.sendTemplateSms("your template id", tencentSmsRequest);
    }
}

(5) The transmission

Access localhost: 8080 / sayHello

image

3. PengzuSmsService Interface

    /**
     *  单个发送短信
     * @param params 根据对应的短信服务商所需信息填写
     */
    public Object sendSms(Integer type,Object params) throws HTTPException, IOException, ClientException;

    /**
     * 单个发送模板短信
     * @param tempalteId 短信模板id
     * @param params 根据对应的短信服务商所需信息填写
     */
    public Object sendTemplateSms(String tempalteId, Object params) throws HTTPException, IOException, ClientException;

    /**
     *  批量发送短信
     * @param params 根据对应的短信服务商所需信息填写
     */
    public Object sendBatchSms(int type,Object params) throws HTTPException, IOException, ClientException;

    /**
     * 批量发送模板短信
     * @param tempalteId 短信模板id
     * @param params 根据对应的短信服务商所需信息填写
     */
    public Object sendBatchTemplateSms(String tempalteId, Object params) throws HTTPException, IOException, ClientException;

This interface provides a single and bulk SMS text messages and templates, note that currently only provides a synchronous transmission method, sending asynchronous method, combined with the thread pool.

to sum up

Just be tested for Tencent SMS services, SMS services Ali did not really verify, I hope to complete the verification junior partner to work together to improve the starter, the starter project please find it useful. If you want to use Tencent cloud messaging business, then you can use in accordance with Demo.

github address: the SMS-the Spring-the Boot-Project

Author: jackieonway
link: https: //www.jianshu.com/p/4a3fff5cb2d6
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/smfx1314/p/11130145.html