一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot整合阿里云短信服務的方法

SpringBoot整合阿里云短信服務的方法

2022-02-25 00:43一生酷到底 Java教程

在實際項目中經常有發送短信的功能,今天進說一下SpringBoot整合阿里云短信服務的相關知識,新建短信微服務,編寫發送短信接口的方法文中給大家介紹的很詳細,需要的朋友參考下吧

一、新建短信微服務

1、在service模塊下創建子模塊service-msm

2.創建controller和service代碼

3.配置application.properties

?
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.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
  #最大阻塞等待時間(負數表示沒限制)
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、創建啟動類

?
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.在service-msm的pom中引入依賴

?
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>

2.編寫controller,根據手機號發送短信

?
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("發送短信失敗");
        }
    }
}

3.編寫service

?
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
@Service
public 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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产肥老上视频 | 吉泽明步高清无码中文 | 亚洲国产欧美目韩成人综合 | 亚洲黄色三级视频 | 国产va免费精品高清在线观看 | 久久99精国产一区二区三区四区 | 国产免费久久精品 | 日韩精品欧美高清区 | japan孕妇孕交 | 99在线免费播放 | 狠狠涩| 亚洲国产精品综合一区在线 | 国产成人精品1024在线 | 鸥美毛片| 日本高清视频在线免费观看 | 好吊色青青青国产综合在线观看 | 99re热精品这里精品 | chinese男性厕所撒尿合集 | 亚洲电影成人 成人影院 | 18美女光胸光屁屁洗澡 | 久久91精品国产91久久户 | 边摸边吃奶又黄激烈视频韩国 | 久久精品手机观看 | 免费观看日本人成影片 | 久久久久久久久女黄9999 | 日本无卡码一区二区三区 | 2018久久精品热在线观看 | 高h辣文小说网 烧书阁 | 成年女人毛片免费观看97 | 国产一区二 | 亚瑟天堂久久一区二区影院 | 亚洲乱人伦在线 | 亚洲精品资源在线 | 久久受www免费人成_看片中文 | 精品国产欧美一区二区三区成人 | 亚洲精品国产精品国自产观看 | 男女发生性关系视频 | 性欧美高清强烈性视频 | 精品午夜久久福利大片免费 | 天天射夜夜爽 | 国产精品福利 |