SpringBoot整合阿里云短信服務(wù)的方法
一、新建短信微服務(wù)
1、在service模塊下創(chuàng)建子模塊service-msm
2.創(chuàng)建controller和service代碼
3.配置application.properties
# 服務(wù)端口 server.port=8006 # 服務(wù)名 spring.application.name=service-msm # mysql數(shù)據(jù)庫連接 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root spring.redis.host=192.168.44.131 spring.redis.port=6379 spring.redis.database= 0 spring.redis.timeout=1800000 spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=-1 #最大阻塞等待時間(負數(shù)表示沒限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0 #最小空閑 #返回json的全局時間格式 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 #配置mapper xml文件的路徑 mybatis-plus.mapper-locations=classpath:com/atguigu/cmsservice/mapper/xml/*.xml #mybatis日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4、創(chuàng)建啟動類
@ComponentScan({"com.south"}) @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消數(shù)據(jù)源自動配置 public class ServiceMsmApplication { public static void main(String[] args) { SpringApplication.run(ServiceMsmApplication.class, args); } }
二、阿里云短信服務(wù)
幫助文檔:
https://help.aliyun.com/product/44282.html?spm=5176.10629532.0.0.38311cbeYzBm73
三、編寫發(fā)送短信接口
1.在service-msm的pom中引入依賴
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> </dependency> </dependencies>
2.編寫controller,根據(jù)手機號發(fā)送短信
@CrossOrigin //跨域 public class MsmApiController { @Autowired private MsmService msmService; @Autowired private RedisTemplate<String, String> redisTemplate; @GetMapping(value = "/send/{phone}") public R code(@PathVariable String phone) { String code = redisTemplate.opsForValue().get(phone); if(!StringUtils.isEmpty(code)) return R.ok(); code = RandomUtil.getFourBitRandom(); Map<String,Object> param = new HashMap<>(); param.put("code", code); boolean isSend = msmService.send(phone, "SMS_180051135", param); if(isSend) { redisTemplate.opsForValue().set(phone, code,5,TimeUnit.MINUTES); return R.ok(); } else { return R.error().message("發(fā)送短信失敗"); } } }
3.編寫service
@Service public class MsmServiceImpl implements MsmService { /** * 發(fā)送短信 */ public boolean send(String PhoneNumbers, String templateCode, Map<String,Object> param) { if(StringUtils.isEmpty(PhoneNumbers)) return false; DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); //request.setProtocol(ProtocolType.HTTPS); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("PhoneNumbers", PhoneNumbers); request.putQueryParameter("SignName", "我的谷粒在線教育網(wǎng)站"); request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); return response.getHttpResponse().isSuccess(); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } return false; } }
到此這篇關(guān)于SpringBoot整合阿里云短信服務(wù)的文章就介紹到這了,更多相關(guān)SpringBoot阿里云短信服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot 整合fluent mybatis的過程,看這篇夠了
這篇文章主要介紹了springboot 整合fluent mybatis的過程,配置數(shù)據(jù)庫連接創(chuàng)建數(shù)據(jù)庫的詳細代碼,本文給大家介紹的非常詳細,需要的朋友可以參考下2021-08-08SpringBoot設(shè)置Session失效時間的解決方案
當(dāng)過期時間是大于1分鐘的時候是沒有什么問題的,但是如果設(shè)置過期時間小于1分鐘,就會失效,這篇文章主要介紹了SpringBoot設(shè)置Session失效時間的解決方案,需要的朋友可以參考下2024-05-05springboot的控制反轉(zhuǎn)和自動裝配示例代碼
這篇文章主要介紹了springboot的控制反轉(zhuǎn)和自動裝配的相關(guān)知識,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06SpringBoot+Mybatis plus實現(xiàn)多數(shù)據(jù)源整合的實踐
本文主要介紹了SpringBoot+Mybatis plus實現(xiàn)多數(shù)據(jù)源整合的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10SpringBoot實現(xiàn)ImportBeanDefinitionRegistrar動態(tài)注入
在閱讀Spring Boot源碼時,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar來實現(xiàn)Bean的動態(tài)注入,它是Spring中一個強大的擴展接口,本文就來詳細的介紹一下如何使用,感興趣的可以了解一下2024-02-02