【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
2016-08-16 10:40
621 查看
Maven+springboot+阿里大于短信验证服务
纠结点:Maven库没有sdk,需要解决
Maven打包找不到相关类,需要解决
ps:最近好久没有写点东西了,项目太紧,今天来一篇
一、本文简介
在工作当中的项目中,我遇到过这种情况,公司架构选型SpringBoot ,这是个基于Maven构建的项目,需要集成阿里大鱼短信系统,然后暴露REST风格的API接口出去。本文重点讲解。
二、开发准备
阿里大于,是阿里的通讯产品之一,提供短信验证码服务。1、需要注册阿里大鱼账户、充值、设置,这一块不多讲,官网有文档。
(https://www.alidayu.com/)
2、下载官网开发包(SDK)
<!-- 阿里大于短信 --> <dependency> <groupId>zhanyun</groupId> <artifactId>zhanyun</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>zhanyun2</groupId> <artifactId>zhanyun2</artifactId> <version>1.0</version> </dependency>
View Code
如果设置失败,请联系WeChat: wixf150
2.3、需要在maven中加入配置插件(这一步非必选)
<!-- 解决本地jar植入的插件 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> <compilerArguments> <extdirs>src\main\webapp\WEB-INF\lib</extdirs> </compilerArguments> </configuration> </plugin>
3、加入SpringBoot需要的依赖
springboot pom.xml.完整的代码如下:
<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>cc-hyh-demo</groupId>
<artifactId>cc.hyh.demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- parent依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
</parent>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<!-- 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 解决本地jar植入的插件 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> <compilerArguments> <extdirs>src\main\webapp\WEB-INF\lib</extdirs> </compilerArguments> </configuration> </plugin>
</plugins>
</build>
<dependencies>
<!-- 阿里云 短信 依赖 -->
<dependency>
<groupId>taobao</groupId>
<artifactId>taobao</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>taobao</groupId>
<artifactId>taobao</artifactId>
<version>1.0</version>
</dependency>
<!-- springboot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
注意:阿里短信的包,是我自己做到本地库中的,不要照搬。
4、主要的服务类
其他次要的代码省略。主要实现发送验证码代码:
package cc.dayu.service; import java.util.Date; public class DaYuService { public Info requestSmsCode(String phone) { //这个只是我项目中的标识,忽略 Info in = new Info(); //阿里大鱼URL String url = "http://gw.api.taobao.com/router/rest"; //我自己随机生成了六位数验证码,自己去实现 String code = RandomUtil.createRandomVcode(); //以下才是重点 三个参数,一个url阿里大鱼的服务地址,其他两个去阿里大鱼后端查看自己的相应的参数 TaobaoClient client = new DefaultTaobaoClient(url, "23334143", "23gyfruy38hf83yh7y8u98j9u9j9i9"); // String json="{\"code\":\"1234\",\"product\":\"某某电子商务验证\"}"; AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest(); req.setExtend("1"); //必须填写normal req.setSmsType("normal"); //你应用的名字 req.setSmsFreeSignName("XXXX"); //电话号码 req.setRecNum(phone); //模板 req.setSmsTemplateCode("SMS_8926569"); //模板中的参数,按照实际情况去 req.setSmsParamString("{msg:'" + code + "'}"); try { AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req); // 这里是我设置的一个保存验证码 机制。按照实际需求,自行设计 UserSms userSms = new UserSms(); userSms.setPhone(phone); userSms.setCode(code); userSms.setTime(new Date()); sms.addSms(userSms); in.setStatus("发送成功"); } catch (ApiException e) { // TODO Auto-generated catch block // e.printStackTrace(); in.setStatus("发送失败"); } return in; } }
四、交流方式
其实对于Maven项目添加阿里大鱼这样的Demo,主要的问题在于,如何将阿里大鱼jar加入maven,并且能打包成功,需要源码或交流沟通,请加WX: wixf150 (一路上)
转发请注明出处(原创):http://www.cnblogs.com/hyhnet/p/5775473.html
相关文章推荐
- 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
- Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
- [置顶] 【微框架实战】Maven+SpringBoot 短信接口详解与Demo
- 阿里大鱼短信接口整合Tp3.2.3开发整理
- php短信接口(阿里大鱼)
- 集成阿里大于发送短信接口
- 阿里大鱼短信接口PHP版,精简版阿里大鱼短信SMS发送接口PHP实例
- php版阿里大于(阿里大鱼)短信发送实例详解
- Java调用 阿里大鱼 短信接口
- 阿里最新热修复框架Sophix集成详解
- Maven集成阿里大鱼短信
- thinkphp3.2 整合阿里大鱼短信发送接口
- io移动开发,云通讯短信验证接口demo详解如,有疑问欢迎与博客评论留言与我探讨
- .NET深入解析LINQ框架(五:IQueryable、IQueryProvider接口详解)
- java_集合体系之Collection框架相关抽象类接口详解、源码——02
- 【网站国际化必备】Asp.Net MVC 集成Paypal(贝宝)快速结账 支付接口 ,附源码demo
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- java_集合体系之Map框架相关抽象类接口详解、源码——08
- 网站上集成支付宝接口(一、申请合作者ID和key,下载Demo)