一个简易上手的短信服务Spring Boot Starter

640?wx_fmt=png

作 者:jackieonway

来 源:jianshu.com/u/36510c75d37c

广而告之:由于此订阅号换了个皮肤,系统自动取消了读者的公众号置顶。导致用户接受文章不及时。可以打开订阅号,选择置顶(标星)公众号,重磅干货,第一时间送达!

短信服务在用户注册、登录、找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式让用户进行操作,从而提高用户的实用性。

由于 Spring boot 的约定大于配置的理念,使得在使用Spring变得更加方便。Spring Boot 项目组提供了很多Starter ,让我们在使用 Spring 的时候变得非常容易。对于官方提供的Starter 采用 spring-boot-starter-xxx开头,对于非官方提供的Spring Boot Starter ,官方建议采用 xxxx-spring-boot-starter命名。

1. 开发工具及编译

IntelliJ IDEA 2018.2.5

2. 如何使用sms-spring-boot-starter

(1)  在pom文件中引入

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

在pom.xml中配置maven中央仓库Snapshots地址

<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) 在application.yml中加入

spring:	
 pengzu:	
   sms:	
     sms-type: tentcent  	
     security-key: your security-key 	
     appid: your appid 	
     sign: your sign 

(3) 在Springboot主程序中 加入

@EnabledPengzuSmsAutoConfiguration	
@ComponentScan(basePackages = {"com.example.demo.sms", "com.pengzu.sms"}) 

(4) 创建发送短信程序

@RestController	
public class HelloController {	
    @Autowired	
    private PengzuSmsService pengzuSmsService;	

	
    @GetMapping("/sayHello")	
    public Object sayHello() throws ClientException, HTTPException, IOException {	
       	
        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) 发送

访问 localhost:8080/sayHello

640

PengzuSmsService接口

  /**	
     *  单个发送短信	
     * @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;

该接口提供了单个和群发短信与模板短信,注意目前只提供了同步发送方法,异步发送方法,请结合线程池进行。

只是针对腾讯短信服务进行了试验,阿里的短信服务并未真正验证,希望各位小伙伴能合作完成验证,共同完善该starter,觉得有用请starter该项目。如果只想使用而腾讯云短信业务的话,按照Demo使用即可。

github地址:sms-spring-boot-project

           

640?

标星订阅号示意图

1. 

2. 

3. 

4. 

640?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值