作者简介:大家好,我是郭嘉烨



目录

  • ​​前言​​
  • ​​原代码​​
  • ​​优化​​
  • ​​添加maven依赖​​
  • ​​创建config.properties配置文件​​
  • ​​创建applicationContext.xml配置文件​​
  • ​​封装工具类​​
  • ​​测试类​​

前言


上一篇简单的实现了短信的验证码功能,今天主要对上篇的代码进行优化,这样方便运用到具体的实例当中。



原代码

// This file is auto-generated, don't edit it. Thanks.

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;

public class Sample {
public static void main(String[] args_) throws Exception {
Config config = new Config()
//这里修改为我们上面生成自己的AccessKey ID
.setAccessKeyId("LTAI5tLdwwPpCrJbzMdTdQ7")

//这里修改为我们上面生成自己的AccessKey Secret
.setAccessKeySecret("jnP9no9KhtsE4kVbqbV40JKCksCqy3");
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
Client client = new Client(config);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName("阿里云短信测试")//短信签名
.setTemplateCode("SMS_154950909")//短信模板
.setPhoneNumbers("157xxxxxxxx")//这里填写接受短信的手机号码
.setTemplateParam("{\"code\":\"1234\"}");//验证码
// 复制代码运行请自行打印 API 的返回值
client.sendSms(sendSmsRequest);
}
}

优化

添加maven依赖

<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--spring的ioc相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!--spring的jdbc相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>

<!--阿里云短信依赖-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.9</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--spring整合单元测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>

创建config.properties配置文件


在原代码中,我们用到的AccessKey ID、AccessKey Secret、域名。短信签名以及短信模板都是固定不变的,因此我们可以将它们拿出来放到一个配置文件中。


#aliyun短信配置参数
aliyun.accessKeyId=LTAI5tLdwwPpCrJbzMdTdQ7w
aliyun.accessKeySecret=jnP9no9KhtsE4kVbqbV40JKCksCqy2
aliyun.endpoint=dysmsapi.aliyuncs.com
aliyun.signName=阿里云短信测试
aliyun.templateCode=SMS_154950909

创建applicationContext.xml配置文件


在配置文件中配置注解自动扫描,以及加载外部配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 注解扫描 -->
<context:component-scan base-package="com.it"/>
<!--加载外部配置-->
<context:property-placeholder location="classpath:config.properties"/>
</beans>

封装工具类

package com.it.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component //创建当前类的对象
public class MessageTemplate {

@Value("${aliyun.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.endpoint}")
private String endpoint;
@Value("${aliyun.signName}")
private String signName;
@Value("${aliyun.templateCode}")
private String templateCode;


public void sendMessage(String phone,String code) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = endpoint;
Client client = new Client(config);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName(signName)
.setTemplateCode(templateCode)
.setPhoneNumbers(phone)
.setTemplateParam("{\"code\":\""+code+"\"}");
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
SendSmsResponseBody body = sendSmsResponse.getBody();
System.out.println("短信发送结果:"+body.toString());//打印结果
}
}

测试类

package com.it.sms;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;
//spring整合单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MessageTemplateTest {

@Autowired
private MessageTemplate messageTemplate;

@Test
public void sendMessage() {

try {
messageTemplate.sendMessage("17809523930","1024");
} catch (Exception e) {
e.printStackTrace();
}
}
}




????

⭐️

✏️