调用阿里的短信接口!总结,精辟!!!

接口的调用

总结:首先在pom文件中加入阿里公司的jar包,利用springmvc框架写一个方法,在所需要的地方调用即可。详细的步骤请看下面的说明。

1.在项目pom文件中加入阿里的jar包

2、在配置文件中加入公司开的户,和链接!短信发送一般都是需要收取费用的。注意,配置文件一定要加载到框架的容器中,方便在代码中取值。

3、控制层的代码!!!!!!

/**
 * 短信的控制层
 *title:
 *@author taotk
 *@2016年8月12日
 *@company
 */
@Controller
@RequestMapping("/sms")
public class MessageController extends BaseController {

private static Logger log = LoggerFactory.getLogger(MessageController.class.getName());
    @Autowired
    private MessageService smsService;

@RequestMapping(value = "/code/{type}/{phone}")
    public MappingJacksonJsonView register(@PathVariable String type, @PathVariable String phone,
            HttpServletRequest request) throws ApiException {
        log.info("======================sms.code=================");
        MappingJacksonJsonView mv = new PinjiaMappingJacksonJsonView();
        String sms_model = null;
        switch (ESMSType.valueof(Integer.parseInt(type))) {
        case Active:
            sms_model = Resources.getString("appSMSActiveModelId");
            break;
        case Change:
            sms_model = Resources.getString("appSMSChangeModelId");
            break;
        case Id:
            sms_model = Resources.getString("appSMSIdModelId");
            break;
        case Register:
            sms_model = Resources.getString("appSMSRegisterModelId");
            break;
        case Pass:
            sms_model = Resources.getString("appSMSPassModelId");
        default:
            break;
        }
        String dataType = Resources.getString("appSMSDataFormat");
        ESMSDataType dataFormat = ESMSDataType.Json;
        if (!"json".equals(dataType))
            dataFormat = ESMSDataType.Xml;
        smsService.getCode(ESMSType.valueof(Integer.parseInt(type)), sms_model, phone, null, dataFormat);
        return mv;
    }

4、service层的代码

@Service
public class MessageService {

// 常量参数
        final String appKey = Resources.getString("appSMSId");
        final String secret = Resources.getString("appSMSKey");
        final String url = Resources.getString("appSMSUrl");

/**
         * 获得手机验证验证码
         *
         * @param code_type
         *            获取短信验证码类别
         * @param sms_model
         *            短信模板
         * @param sms_phone
         *            需要发送验证码的手机
         * @param sms_type
         *            短信类型,默认为文字短信
         * @param data_Type
         *            返回数据类型
         * @return {@link String}
         * @throws ApiException
         */
        public String getCode(ESMSType code_type, String sms_model, String sms_phone, String sms_type,
                ESMSDataType data_Type) throws ApiException {
            TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret, data_Type.getTitle());
            AlibabaAliqinFcSmsNumSendRequest smsRequest = new AlibabaAliqinFcSmsNumSendRequest();
            smsRequest.setSmsType(StringUtils.isEmpty(sms_type) ? "normal" : sms_type);
            smsRequest.setSmsFreeSignName(code_type.getTitle());
            smsRequest.setRecNum(sms_phone);
            String message =":-,尊敬的用户您好,拼家系统为您注册了一个账户。账户:"+sms_phone+",密码:"+sms_phone+",您可以登录拼家网,关注您的装修流程";
            smsRequest.setSmsParamString("{\"code\":\"" + message + "\",\"product\":\"" + "【齐家网】"+ "\"}");
            smsRequest.setSmsTemplateCode(sms_model);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(smsRequest);
            return null;
        }
}
5、其他的是一些工具类,和枚举的类。

(1)

public class Resources {
    private static Logger log = LoggerFactory.getLogger(Resources.class);
    /** 国际化资源 */
    public static ResourceBundle resourceBundle;
    public static ResourceBundle wf;
    public static ResourceBundle messageBundle;

static {
        resourceBundle = ResourceBundle.getBundle("application");
        messageBundle = ResourceBundle.getBundle("message");
    }

public static void close() {
        resourceBundle = null;
    }

public static String myString() {
        return resourceBundle.toString();
    }

/**
     * 从资源文件中返回字符串 我们不希望程序崩溃,所以如果没有找到Key,就直接返回Key
     */
    public static String getWebMessage(String key) {
        try {
            if (!messageBundle.containsKey(key)) {
                return "";
            }
            return messageBundle.getString(key);
        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
            return "";
        }
    }

public static String getWorkflow(String bizType, String key) {
        wf = ResourceBundle.getBundle(bizType + "_wf");
        return wf.getString(key);
    }

public static String getErrorMessage(String key) {
        try {
            if (!messageBundle.containsKey(key)) {
                return "";
            }
            return  messageBundle.getString(key);
        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
            return "";
        }
    }

public static String getString(String key) {
        try {
            if (!resourceBundle.containsKey(key)) {
                return "";
            }
            return resourceBundle.getString(key);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return "";
        }
    }

public static int getConfigAsInt(String key) {
        return Integer.valueOf(getString(key));
    }

/**
     * 从资源文件中返回字符串 我们不希望程序崩溃,所以如果没有找到Key,就直接返回Key
     */
    public static String getString(String key, Object[] args) {
        try {
            return MessageFormat.format(getString(key), args);
        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
            return "";
        }
    }
}

(2)

public class PinjiaMappingJacksonJsonView extends MappingJacksonJsonView {

protected Object filterModel(Map<String, Object> model) {
        Map<?, ?> result = (Map<?, ?>) super.filterModel(model);
        if (result.size() == 1) {
            return result.values().iterator().next();
        } else {
            return result;
        }
    }
}
(3)

/**
 * 文字短信验证码类型枚举类
 *title:
 *@author taotk
 *@2016年8月12日
 *@company www.51pinjia.com
 */
public enum ESMSType {
    Register("注册验证"), Login("登录验证"), Change("变更验证"), Id("身份验证"), Active("活动验证"), Pass("变更验证");
    private String title;

private ESMSType(String title) {
        this.title = title;
    }

public static ESMSType valueof(int index) {
        return ESMSType.values()[index];
    }

public String getTitle() {
        return title;
    }
}

(4)

/**
 * 文字短信验证码数据类型类型枚举类
 *title:
 *@author taotk
 *@2016年8月12日
 *@company www.51pinjia.com
 */
public enum ESMSDataType {
    Json("json"), Xml("xml");
    private String title;

private ESMSDataType(String title) {
        this.title = title;
    }

public static ESMSDataType valueof(int index) {
        return ESMSDataType.values()[index];
    }

public String getTitle() {
        return title;
    }
}

时间: 2024-08-08 09:47:25

调用阿里的短信接口!总结,精辟!!!的相关文章

Jmeter调用阿里大于短信接口

话不多说,直接贴代码 1 package cn.litry.sms; 2 3 import org.apache.jmeter.config.Arguments; 4 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 5 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 6 import org.apache.jme

阿里大鱼短信接口整合Tp3.2.3开发整理

阿里大鱼 http://www.alidayu.com/ 的短信接口总体是不错的,别安驹个人认为不管是从性价比还是稳定性上都是跟同类的短信接口好些,毕竟是大公司的东西不会差到哪去.下面把之前开发的短信接口做个整理. 1,登陆阿里大鱼的管理中心新增自己的应用,然后使用什么模板请提交审核,此处不做多说. 2,核心步骤,整理阿里大鱼给出的php示例核心包,不过别安驹已经为你整理好了只需要下载即可  核心包传送门 (ps:解压密码:http://www.bieanju.com/). 3,前台调用发送短信

对接阿里云短信接口

阿里云短信服务 简单说明 之前使用过阿里的阿里大于这个短信代发服务,今天使用的是阿里云最新的一个短信代发服务,文档真的有点不详细,加上网上的博客也太少了,几经蹉跎还是把它给搓出来了,供大家学习 阿里云短信 首先没用过的玩家需要去阿里云注册账户,然后开启两个私钥,这两个密钥是我们的必需品 然后去申请签名和模版,这个也是我们的必需品 现在不怎么好申请了,建议申请理由都写成个人项目测试啥的,过不了就多申请几次,我申了三次才ojbk! 上面两个环境已经装备好了的你,怎么可以吃霸王餐呢?当然是充钱进去呀,

TP3.2.3 接入阿里sms 短信接口

阿里云短信接口 配置文件 config.php //阿里大鱼 'Ali_SMS' =>array( 'sms_temp' =>'短信模板', 'sms_sign' =>'签名', 'appkey' =>'appkey', 'secretKey'=>'secretKey', ), TestController.php Vendor('alisms.Alisms'); $alisms = new \Alisms(C('Ali_SMS.appkey'),C('Ali_SMS.sec

阿里大鱼短信接口 for Thinkphp

不得不说阿里云为创业者在起步时期提供了不少便利,短信接口不管是web应用还是APP都必不可少.大鱼一张口,这是要生吞其他小虾米的节奏啊,四分的价格很便宜了,现在注册会送60元代金券,时间不到一个月,也就是让你免费调试,短信发送效果很好,基本能够达到承诺的三秒,一般情况两三秒,具体效果还待检验,好了,直奔主题. 第一步:注册阿里大鱼账号:http://www.alidayu.com 第二步:进入<开发者控制台>--><应用管理>中创建应用,应用标签选择"阿里大鱼&qu

调用阿里云短信服务

package com.example.demo.untils; /** * Created by JQY on 2019/5/15 */ import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.Send

THINKPHP3.2.3增加阿里云短信接口思路整理

https://help.aliyun.com/document_detail/55359.html?spm=5176.product44282.4.7.O4lc1n 阿里云短信服务地址,感冒的下载看看 1 文件存放位置的问题 阿里云的短信接口 属于第三方扩展 所以文件放在 thinkphp\vendor\ 比如起名message 把SDK的文件都存到里面 2 把实现功能的代码 写好 放在message目录 不过注意文件的路径 代码里面的文件头 引用的文件地址用类似的 require_once

阿里大鱼短信接口

阿里大于短信验证实现完整代码分享 http://bbs.2ccc.com/topic.asp?topicid=515649 补充:跨平台的例子可以去csdn下载这位大神的demo http://download.csdn.net/detail/hansxia888/9603061 1 这两天搞那个sign,搞的烦躁,问了一下论坛的那位同学,开口500. 2 唉,delphi这种偏门的就是这样,啥都贵. 3 后来想想用C#按阿里那边的帮助文档写了个,可以正常发送.然后把C#翻译成Delphi就可以

阿里大于短信接口提示500错误的解决方法

这两天做公司项目的邀请加入页面时需要调试阿里大于的短信接口,因为项目中其他地方也有用到这个接口,老大已经写好了,于是我就直接拿过来用了,可是不管怎么调试服务器那边都是一直返回500错误[无可用用户信息],在网上搜了好多资料来看,可是都没有找到相关的,因为之前做另一个项目时自己也亲自接入过这个接口,当时是可以用的,于是先在本地试了下那个项目的大于接口,发现是正常的,于是就把那个项目的代码直接搬了过来,可是原来好好的代码一般到了现在项目里面就不能用了,真的是头痛死了,调的心里都烦了,于是就想着先跳过