在实际项目中经常有发送短信的功能,今天进说一下SpringBoot整合阿里云短信服务的相关知识,新建短信微服务,编写发送短信接口的方法文中给大家介绍的很详细,需要的朋友参考下吧
在实际项目中经常有发送短信的功能,今天进说一下SpringBoot整合阿里云短信服务的相关知识,新建短信微服务,编写发送短信接口的方法文中给大家介绍的很详细,需要的朋友参考下吧
在实际项目中经常有发送短信的功能,今天进说一下SpringBoot整合阿里云短信服务的相关知识,新建短信微服务,编写发送短信接口的方法文中给大家介绍的很详细,需要的朋友参考下吧
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# 服务端口server.port=8006# 服务名spring.application.name=service-msm# mysql数据库连接spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8spring.datasource.username=rootspring.datasource.password=rootspring.redis.host=192.168.44.131spring.redis.port=6379spring.redis.database= 0spring.redis.timeout=1800000spring.redis.lettuce.pool.max-active=20spring.redis.lettuce.pool.max-wait=-1 #最大阻塞等待时间(负数表示没限制)spring.redis.lettuce.pool.max-idle=5spring.redis.lettuce.pool.min-idle=0 #最小空闲#返回json的全局时间格式spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.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 |
|
1
2
3
4
5
6
7
|
@ComponentScan({"com.south"})@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置public class ServiceMsmApplication { public static void main(String[] args) { SpringApplication.run(ServiceMsmApplication.class, args); }} |
帮助文档:
https://help.aliyun.com/product/44282.html?spm=5176.10629532.0.0.38311cbeYzBm73
|
1
2
3
4
5
6
7
8
9
10
|
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> </dependency> </dependencies> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@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("发送短信失败"); } }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
@Servicepublic class MsmServiceImpl implements MsmService { /** * 发送短信 */ 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", "我的谷粒在线教育网站"); 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; }} |
到此这篇关于SpringBoot整合阿里云短信服务的文章就介绍到这了,更多相关SpringBoot阿里云短信服务内容请搜索米米素材网以前的文章或继续浏览下面的相关文章希望大家以后多多支持米米素材网!
原文链接:https://blog.csdn.net/weixin_43118617/article/details/120898762
发表评论