Preface
Usually when we are doing development, we will encounter the need to send text messages and emails. The customer often provides a template for the sending content. If we splice strings in the program to get this template, it is obviously a way to cheat our teammates. Generally, the template is placed in the properties file and replace some of the variables when used.
In this article, we use springboot to implement the function of sending SMS verification codes based on the template. I won’t say much below, let’s take a look at the detailed introduction together.
Tips:
1. Regular expressions
2. Springboot reads properties file
Template definition
Define the SMS templates that need to be defined in the msg.properties file, with the same directory as application.properties. Note that the [[code]] in it is the variable to be replaced.
tem.msg.verify.code=The verification code is: [code], please do not disclose it to others.
Read properties
Define component MSGConstants, specify the properties file to be loaded, used to read the defined template, use the @Value annotation of spring
@PropertySource("classpath:msg.properties")@Componentpublic class MSGConstatns { @Value("${tem.msg.verify.code}") private String sendCodeMsg; public String getSendCodeMsg() { return sendCodeMsg; } public void setSendCodeMsg(String sendCodeMsg) { this.sendCodeMsg = sendCodeMsg; }}Analyze template tool class
Considering the common, set the parameter to Map, that is, the variable that needs to be replaced, and the regular expression replaces the corresponding key. The format of the key here is: {key}, which can be modified according to your own situation and modify the regular at the same time.
public static String getContent(Map<String, String> params,String content) { String reg = "//{//w*}";// Pattern pattern = Pattern.compile(reg); Matcher matcher = pattern.matcher(content); while (matcher.find()) { String group = matcher.group();// String key = group.substring(1, group.length() - 1); if (!params.containsKey(key)) throw new NormalException("No key to be replaced was found:" + key); content = content.replace(group, params.get(key)); } return content; }test
A very simple ajax request returns the received text message content
@RestController@RequestMapping("demo")public class DemoController { @Resource private MSGConstatns msgConstatns; @RequestMapping("msg") public String msgContent(){ String code = "123456";//In formal development, random numbers are generally used Map<String,String> params = new HashMap<>(); params.put("code",code); return SendCodeUtil.getContent(params,msgConstatns.getSendCodeMsg()); }}result
Expected value: Verification code is: 123456, please do not disclose it to others
Actual effect:
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.