preface
Last article talked about the Springboot integrated mail service, next let’s learn how to use SMS service in the Springboot project. The short message service in the project will be used basically, simple registration verification code, message notification and so on will be used. So I’m going to inherit the SMS service as well. The platform I use for short message service is Ali Cloud. There are many SMS service providers on the Internet. You can choose according to your own needs.
The preparatory work
Service opening and configuration on Ali Cloud. These ali cloud official documentation all write very clear, do not go, you can consult this article: https://blog.csdn.net/qq_27790011/article/details/78339856
After configuration, you need to obtain the following information:
AccessKeyId and accessSecret are secret keys. It can be found in user AccessKey.
SignName is the signature name.
TemplateCode is the templateCode
Add dependencies and configurations
With this in mind, let’s start developing in our project. Add a dependency to the pom.xml file as well:
<! --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun- Java - sdK-core </artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> The < version > 1.2.61 < / version > < / dependency >Copy the code
This fastjson dependency is not required, depending on whether you use it in your project. If not, add the first dependency.
Then add the configuration to the application.properties file. These four parameters are the ones we obtained in preparation.
The service layer
As with the mail service, we don’t involve the database here. We will write the Service layer directly and create the SmsService interface and SmsServiceImpl class.
The code for SmsServiceImpl is as follows:
@Service @Slf4j public class SmsServiceImpl implements SmsService { @Value("${sms.accessKeyId}") private String accessKeyId; @Value("${sms.accessSecret}") private String accessSecret; @Value("${sms.signName}") private String signName; @Value("${sms.templateCode}") private String templateCode; @Override public boolean sendSms(String iponeNUmber) { DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", 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", "cn-hangzhou"); request.putQueryParameter("PhoneNumbers", iponeNUmber); request.putQueryParameter("SignName", signName); request.putQueryParameter("TemplateCode", templateCode); JSONObject object=new JSONObject(); String randCode=getRandCode(6); Log.info (" verification code: {}",randCode); object.put("code",randCode); request.putQueryParameter("TemplateParam", object.toJSONString()); try { CommonResponse response = client.getCommonResponse(request); log.info(response.getData()); return true; } catch (Exception e) { log.error("{}",e); } return false; Public static String getRandCode(int digits) {StringBuilder sBuilder = new StringBuilder(); Random rd = new Random((new Date()).getTime()); for(int i = 0; i < digits; ++i) { sBuilder.append(String.valueOf(rd.nextInt(9))); } return sBuilder.toString(); }}Copy the code
The overall code logic is simple, starting with the Value annotation to retrieve the four parameters configured in the configuration file.
In sendSms() :
DefaultProfile and IAcsClient are instances of DefaultAcsClient created and initialized. The three parameters correspond to region ID, AccessKey ID of RAM account, and AccessKey Secret of RAM account respectively.
DescribeInstancesRequest creates AN API request and sets parameters. Request.putqueryparamete () We’re modifying mainly the parameters here. PhoneNumbers is the phone number to receive the message. In this case, I send the SMS verification code. So I’m going to generate a 6 bit SMS captcha here. Specific requirements can be adjusted according to requirements.
The controller layer
SmsController: SmsController: SmsController: SmsController: SmsController: SmsController
@RestController @RequestMapping("/sms") public class SmsController { @Autowired private SmsService smsService; @RequestMapping(value = "/send",method = RequestMethod.GET) public String sendSms(@RequestParam(value = "userName")String userName){ smsService.sendSms(userName); return "success"; }}Copy the code
test
Now that the SMS service is set up, let’s test it. We start the project first and call the interface:
http://localhost:9090/zlflovemm/sms/send?userName=13265459362Copy the code
Then look at the log
Look see we got a text on our phones.
You can see that the SMS service is configured successfully. It’s not as complicated as we thought.
One day
Well, that’s all. Today the project code is synced to Github. Making address: https://github.com/QuellanAn/zlflovemm
Follow-up fuel ♡
Welcome to pay attention to personal public account “Programmers love yogurt”
Share all kinds of learning materials, including Java, Linux, big data, etc. Information includes video documents and source code, and share myself and deliver quality technical blog posts.
Be sure to follow and share if you like ❤
This article is published by OpenWrite!