Spring boot uses properties to define the short message template method tutorial, springproperties
Preface
We usually encounter requirements such as text message sending and email sending during development. The sending content is often provided by the customer as a template. If We splice strings in the program to fix this template, obviously, it is a kind of practices for teammates. Generally, the template is put into the properties file and some variables can be replaced when used.
In this article, we use springboot to implement the function of sending text message verification codes based on the template. I will not talk about it here. Let's take a look at the details.
Tips:
1. Regular Expression
2. springboot reads the properties File
Template Definition
Define the SMS template to be defined in the msg. properties file. The directory is the same as application. properties. Note that [code] is the variable to be replaced.
Tem. msg. verify. code = verification code: [code]. Do not disclose it to others.
Read properties
Define the MSGConstants component and specify the properties file to be loaded 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; }}
Parsing template Tool
For public use, set the parameter to Map, that is, the variable to be replaced. Replace the regular expression with the corresponding key. Here, the key format is {key }, you can modify the regular expression as needed.
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 ("the key to be replaced is not found:" + key); content = content. replace (group, params. get (key) ;}return content ;}
Test
A simple ajax request returns the obtained text message content
@ RestController @ RequestMapping ("demo") public class DemoController {@ Resource private MSGConstatns msgConstatns; @ RequestMapping ("msg") public String msgContent () {String code = "123456 "; // In formal development, the random Map <String, String> params = new HashMap <> (); params is generally used. put ("code", code); return SendCodeUtil. getContent (params, msgConstatns. getSendCodeMsg ());}}
Result
Expected Value: The verification code is 123456. Do not disclose it to others.
Actual results:
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.