javascript
一个简易上手的短信服务Spring Boot Starter,连傻瓜都会!
作 者: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
Maven 3.5
JDK 1.8
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?{@Autowiredprivate 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
3?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
總結
以上是生活随笔為你收集整理的一个简易上手的短信服务Spring Boot Starter,连傻瓜都会!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为发布会: 牛逼鸿蒙,吹水的大会
- 下一篇: 通过源码告诉你,阿里的RocketMQ事