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

接口的调用

总结:首先在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;
    }
}

 


    
   

 

原文地址:https://www.cnblogs.com/taotingkai/p/5765940.html