项目描述
上传时间
浏览人数
一,配置Pom文件,导入Maven依赖
-------------------------------------
<!--短信依赖-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
-------------------------------------
二,代码演示
-------------------------------------
//controller层
@Autowired
private MessageService messageService;
//发短信功能send message
@PostMapping("sendMessage")
public JsonMsgPojo sendMessage(String phoneNumbers) {
Map<String, String> contentMap = new HashMap<>();
//String phoneNumbers 接收短信的手机号码
//短信签名名称
contentMap.put("signName", "诚筑说");
//短信模板CODE
contentMap.put("templateCode", "SMS_254890710");
//以下是request.setTemplateParam 所需
//内容参数1
contentMap.put("name", "大哥");
//内容参数2
contentMap.put("time", "还有20分钟");
//短信模板变量对应的实际值
contentMap.put("templateParam", "{\"name\":\"" + contentMap.get("name") + "\",\"time\":\"" + contentMap.get("time") + "\"}");
return messageService.sendMessage(phoneNumbers, contentMap);
}
-------------------------------------
//接口
@Service
public interface MessageService {
JsonMsgPojo sendMessage(String phoneNumbers, Map<String, String> contentMap);
}
-------------------------------------
//实现层
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.book.newbook.domain.JsonMsgPojo;
import com.book.newbook.service.MessageService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class MessageServiceImpl implements MessageService {
//application.properties文件配置以下信息(可选)
//spring.messages.accessKeyId = *********(填自己的accessKeyId)
//spring.messages.accessKeySecret = *********(填自己的accessKeySecret)
@Value("${spring.messages.accessKeyId}")
private String ACCESS_KEY_ID;
@Value("${spring.messages.accessKeySecret}")
private String ACCESS_KEY_SECRET;
@Override
public JsonMsgPojo sendMessage(String phoneNumbers, Map<String, String> contentMap) {
DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
/** use STS Token
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // The region ID
"<your-access-key-id>", // The AccessKey ID of the RAM account
"<your-access-key-secret>", // The AccessKey Secret of the RAM account
"<your-sts-token>"); // STS Token
**/
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phoneNumbers);//接收短信的手机号码
request.setSignName(contentMap.get("signName"));//短信签名名称
request.setTemplateCode(contentMap.get("templateCode"));//短信模板CODE
request.setTemplateParam(contentMap.get("templateParam"));//短信模板变量对应的实际值
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
return new JsonMsgPojo<>(200, "发送成功", null);
} catch (ServerException e) {
e.printStackTrace();
return new JsonMsgPojo<>(500, "发送失败", "系统错误");
} catch (ClientException e) {
Map<String, Object> m = new HashMap<>();
m.put("ErrCode", e.getErrCode());
m.put("ErrMsg", e.getErrMsg());
m.put("RequestId", e.getRequestId());
return new JsonMsgPojo<>(500, "发送失败", m);
}
}
}
-------------------------------------
附:阿里云 java SDK官方帮助文档
https://help.aliyun.com/document_detail/112148.html
附:阿里云 短信服务 ,注册获取accessKeyId,官方帮助文档
https://help.aliyun.com/document_detail/71160.html