本文主要是介绍短信微服务验证码测试 SpringBoot + JMS + 阿里云通信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 需求分析
构建一个通用的短信发送服务,接收activeMQ的消息(MAP类型) 消息包括手机号(phoneNumbers)、短信模板号(templateCode)、签名(signName)、参数字符串(templateParam).
2.代码实现
2.1工程搭建
(1)创建工程wjl_sms_service (JAR工程),POM文件引入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.wjl.sms</groupId><artifactId>wjl_sms_service</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.0.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 热部署 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><!-- avtiveMQ依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency><!-- ali短信服务 --><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.0.3</version></dependency></dependencies><!-- jdk 版本 --><properties><java.version>1.7</java.version></properties>
</project>
(2)创建引导类
package com.wjl.sms;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
(3)创建配置文件application.properties
<!-- 服务端口 -->
server.port=8090 <!--acitveMQ的URL -->
spring.activemq.broker-url=tcp://192.168.13.191:61616<!-- key -->
accessKeyId=阿里云
accessSecret=阿里云
2.2短信工具类
package com.wjl.sms;//@RestController
@Component
public class SmsUtil {@Autowiredprivate Environment env;/*** * @param mobile 待发送手机号* @param template_code 必填:短信签名-可在短信控制台中找到* @param sign_name 必填:短信模板-可在短信控制台中找到* @param param 可选:JSON串*/public CommonResponse sendSms(String phoneNumbers,String templateCode,String signName,String templateParam) {String accessKeyId = env.getProperty("accessKeyId");String accessSecret = env.getProperty("accessSecret");DefaultProfile profile = DefaultProfile.getProfile("default",accessKeyId,accessSecret);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", "default");request.putQueryParameter("PhoneNumbers", phoneNumbers);request.putQueryParameter("SignName", signName);request.putQueryParameter("TemplateCode", templateCode);request.putQueryParameter("TemplateParam", templateParam);request.putQueryParameter("SmsUpExtendCode", "667");request.putQueryParameter("OutId", "LS01");try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());return response;} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return null;}}
2.3消息监听类
package com.wjl.sms;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;import com.aliyuncs.CommonResponse;/*** 收发短息* @author Administrator**/
@Component
public class SmsListener {@Autowiredprivate SmsUtil smsUtil;@JmsListener(destination="sms")public void sendSms(Map<String,String> map) {try {CommonResponse response = smsUtil.sendSms(map.get("phoneNumbers"),map.get("templateCode"),map.get("signName"), map.get("templateParam"));System.out.println("Status:"+response.getHttpStatus());System.out.println("Data:"+response.getData());} catch (Exception e) {e.printStackTrace();}}
}
3. 测试工程
也可以在同一工程测试
application.properties
server.port=8088
<!--acitveMQ的URL -->
spring.activemq.abroker-url=tcp://192.168.13.191:61616
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.wjl.demo</groupId><artifactId>mySpringBootDemo</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.0.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 热部署 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><!-- avtiveMQ依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency></dependencies><!-- jdk 版本 --><properties><java.version>1.7</java.version></properties>
</project>
3.1 测试代码
package com.wjl.demo;import java.util.HashMap;
import java.util.Map;
import java.util.Random;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class SendShortMsg {@Autowiredprivate JmsMessagingTemplate jmsTemplate;@RequestMapping("/sendmsg")public void sendMessage() {Map map=new HashMap();map.put("phoneNumbers", "要发送到的手机号");map.put("templateCode", "SMS_173479773");map.put("signName", "禾硕商城");map.put("templateParam", "{\"code\":\""+getCode()+"\"}");jmsTemplate.convertAndSend("sms",map);}//获取四位随机数记库public String getCode() {Random random=new Random();int num=random.nextInt(1000)+1000;return num+"";}
}
3.2 启动wjl_sms_service 工程和测试工程
地址栏输入:http://localhost:8088/sendmsg
手机可以收到验证码
这篇关于短信微服务验证码测试 SpringBoot + JMS + 阿里云通信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!