欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合阿里云短信服務(wù)的方法

 更新時(shí)間:2021年10月22日 09:58:20   作者:一生酷到底  
在實(shí)際項(xiàng)目中經(jīng)常有發(fā)送短信的功能,今天進(jìn)說(shuō)一下SpringBoot整合阿里云短信服務(wù)的相關(guān)知識(shí),新建短信微服務(wù),編寫(xiě)發(fā)送短信接口的方法文中給大家介紹的很詳細(xì),需要的朋友參考下吧

一、新建短信微服務(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ù)庫(kù)連接
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í)間(負(fù)數(shù)表示沒(méi)限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
  #最小空閑


#返回json的全局時(shí)間格式
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)建啟動(dòng)類(lèi)

@ComponentScan({"com.south"})
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消數(shù)據(jù)源自動(dòng)配置
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

三、編寫(xiě)發(fā)送短信接口

1.在service-msm的pom中引入依賴(lài)

  <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
        </dependency>
    </dependencies>

2.編寫(xiě)controller,根據(jù)手機(jī)號(hào)發(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.編寫(xiě)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot 整合fluent mybatis的過(guò)程,看這篇夠了

    springboot 整合fluent mybatis的過(guò)程,看這篇夠了

    這篇文章主要介紹了springboot 整合fluent mybatis的過(guò)程,配置數(shù)據(jù)庫(kù)連接創(chuàng)建數(shù)據(jù)庫(kù)的詳細(xì)代碼,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • Spring 整合多個(gè)配置文件的方法

    Spring 整合多個(gè)配置文件的方法

    在一些大型應(yīng)用中,可能存在多個(gè)配置文件,這篇文章給大家介紹了Spring 整合多個(gè)配置文件的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2018-05-05
  • Java實(shí)現(xiàn)隊(duì)列的N種方法

    Java實(shí)現(xiàn)隊(duì)列的N種方法

    在Java中,我們可以使用不同的方式來(lái)實(shí)現(xiàn)隊(duì)列,本文主要介紹了Java實(shí)現(xiàn)隊(duì)列的N種方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • SpringBoot設(shè)置Session失效時(shí)間的解決方案

    SpringBoot設(shè)置Session失效時(shí)間的解決方案

    當(dāng)過(guò)期時(shí)間是大于1分鐘的時(shí)候是沒(méi)有什么問(wèn)題的,但是如果設(shè)置過(guò)期時(shí)間小于1分鐘,就會(huì)失效,這篇文章主要介紹了SpringBoot設(shè)置Session失效時(shí)間的解決方案,需要的朋友可以參考下
    2024-05-05
  • springboot的控制反轉(zhuǎn)和自動(dòng)裝配示例代碼

    springboot的控制反轉(zhuǎn)和自動(dòng)裝配示例代碼

    這篇文章主要介紹了springboot的控制反轉(zhuǎn)和自動(dòng)裝配的相關(guān)知識(shí),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • SpringBoot+Mybatis plus實(shí)現(xiàn)多數(shù)據(jù)源整合的實(shí)踐

    SpringBoot+Mybatis plus實(shí)現(xiàn)多數(shù)據(jù)源整合的實(shí)踐

    本文主要介紹了SpringBoot+Mybatis plus實(shí)現(xiàn)多數(shù)據(jù)源整合的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • SpringBoot實(shí)現(xiàn)ImportBeanDefinitionRegistrar動(dòng)態(tài)注入

    SpringBoot實(shí)現(xiàn)ImportBeanDefinitionRegistrar動(dòng)態(tài)注入

    在閱讀Spring Boot源碼時(shí),看到Spring Boot中大量使用ImportBeanDefinitionRegistrar來(lái)實(shí)現(xiàn)Bean的動(dòng)態(tài)注入,它是Spring中一個(gè)強(qiáng)大的擴(kuò)展接口,本文就來(lái)詳細(xì)的介紹一下如何使用,感興趣的可以了解一下
    2024-02-02
  • Java線程池的幾種實(shí)現(xiàn)方法及常見(jiàn)問(wèn)題解答

    Java線程池的幾種實(shí)現(xiàn)方法及常見(jiàn)問(wèn)題解答

    下面小編就為大家?guī)?lái)一篇Java線程池的幾種實(shí)現(xiàn)方法及常見(jiàn)問(wèn)題解答。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件

    Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 淺談Maven的安裝及修改為阿里云下載依賴(lài)

    淺談Maven的安裝及修改為阿里云下載依賴(lài)

    下面小編就為大家?guī)?lái)一篇淺談Maven的安裝及修改為阿里云下載依賴(lài)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05

最新評(píng)論