#调用阿里云通信短信服务
##一 注册阿里云账号
1.注册阿里云账号,进入阿里云通信服务,申请短信签名,短信模板,用户AccessKey,充值。
##二 引入相关依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
##进行demo测试
package com.msg.springbootmsgtest.Controller;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author gh
* @date 2019/12/9 11:05
*/
@RestController
public class MsgController {
/**
*
*/
@RequestMapping("/hello")
public String msgTest(){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "此处为你的accesskey", "此处为你的accesskey密码");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", "此处为你要发送大的手机号");
request.putQueryParameter("SignName", "此处为你的短信签名");
request.putQueryParameter("TemplateCode", "此处为你的短信模板code");
request.putQueryParameter("TemplateParam", "{\"code\":\"此处为你要发送的短信验证码\"}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return "阿里云通信短信测试接口";
}
}
此处可查看api。
大致流程就是这样,这只是一个简单的个人开发demo,具体的可能要用到别的第三方的api接口,根据提供的接口文档进行开发就可以了。本人也是一个小白,刚刚进入这个行业不久,希望大家可以一块学习!
来源:CSDN
作者:当当当当当–
链接:https://blog.csdn.net/weixin_44970278/article/details/103455726