spring集成阿里云短信

该博客内容展示了如何在Java项目中集成阿里云SDK,通过`aliyun-java-sdk-dysmsapi`和`dysmsapi20170525`库来发送短信。代码示例中定义了一个工具类`Sample`,包含了创建阿里云客户端和发送短信的方法。在控制器`SendNoteController`中,调用工具类方法实现短信发送功能。
摘要由CSDN通过智能技术生成

pom依赖

<!-- 阿里云短信SDK -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>
          <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.4</version>
        </dependency>

工具类

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
package io.renren.common.utils;

import com.alibaba.fastjson.JSONObject;
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.*;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import org.springframework.stereotype.Component;

@Component
public class Sample {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
    public String sendNoteMessgae(String PhoneNumbers,String name,String template) throws Exception {
        Client client = Sample.createClient("您的AccessKey ID", "您的AccessKey Secret");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(PhoneNumbers)
                .setSignName("短信签名")
                .setTemplateCode(template)
                .setTemplateParam("{\"name\":\""+name+"\"}");
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);


        return client.sendSms(sendSmsRequest).toString();

    }
}

Controller

import io.renren.common.utils.SendNoteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping(value = "/api/note")
public class SendNoteController {

    @Autowired
    private SendNoteUtil sendNoteUtil;

    @RequestMapping(value = "/sendNote",method = RequestMethod.GET)
    public void sendNote(String phone, HttpServletResponse response){
        String template = "";//更改为自己的短信模板
        try {
            response.getWriter().write(sendNoteUtil.sendNoteMessgae(phone,template));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值