freemarker模板转换

使用场景:

公司业务中需要发送短信/微信/邮件,会有固定的模板,然后使用用户数据替换模板中的数据,这种替换的方式,就用到了freemarker

流程概述

java代码构建传输数据,查询数据库获取到模板,把传输数据替换到模板里,最终拿到替换后的数据,进行短信或者邮件发送

freemarker依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

相关配置

这些配置都用不到,看看就行,如果在classPath下面有模板,就需要配置这些东西,没有就不用配置

server:
  port: 8888
spring:
  application:
    name: freemarker
  freemarker:
    # 是否开启缓存
    cache: false
    # 编码格式
    charset: utf-8
    # 是否暴露request
    expose-request-attributes: true
    # 是否暴露session
    expose-session-attributes: true
    # context暴露为rpc名称
    request-context-attribute: rpc
    # 模板文件后缀
    suffix: .ftl
    # 模板路径
    template-loader-path: classpath:/template/

controller层使用

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wangtuanyuan
 * @Date: 2022/5/15 13:46
 * @Description: 短信模板/微信消息模板/邮件消息模板使用示例
 */
@RestController
public class FkConotroller {

    @GetMapping("/convert")
    public String freemarkerProcess() throws Exception {
        // map的key和value,模拟从别处调过来的数据,传过来时需要把key和value一起传递过来
        Map<String, Object> srcMap = new HashMap<>();
        srcMap.put("e0", "张三");
        srcMap.put("e1", "先生");
        srcMap.put("e2", "11111");
        srcMap.put("e3", "2024");
        srcMap.put("e4", "3");
        srcMap.put("e5", "20");
        srcMap.put("e6", "77777");
        srcMap.put("e7", "8888");

        // 模拟从数据库查询出的模板
        String fmk = "尊敬的${e0}${e1},您好!您申请的尾号${e2}的保险合同“保单贷款”保全业务,已于${e3}年${e4}月${e5}日生效,贷款金${e6}元将于近期转至您尾号为${e7}的账户中,请您注意查收。";

        // 模板转换后的数据,该数据为需要发送的内容
        String string = this.convertFm(srcMap, fmk);

        // 拼装别的数据(比如手机号、邮箱号等),进行短信或者邮件的发送

        // 方法完成,可以不用返回,这里为了方便查看数据
        return string;
    }

    /**
     * @Author wangtuanyuan
     * @Date 2022-05-15 13:38
     * @Param map- 替换到模板内的数据
     * @Param dataForm-需要替换的模板
     * @Description 模拟从数据库查询出模板(dataForm),通过java代码的方式传参数据(map)
     **/
    public String convertFm(Map<String, Object> map, String dataForm) throws IOException, TemplateException {
        // 1.初始化配置类
        Configuration cfg = new Configuration(Configuration.getVersion());
        // 2.指定模板通过字符串方式加载
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
        String templateName = "content";
        stringTemplateLoader.putTemplate(templateName, dataForm);
        cfg.setTemplateLoader(stringTemplateLoader);
        // 3.获取模板,抛出IOException
        Template template = cfg.getTemplate(templateName);
        // 4.解析模板,抛出TemplateException
        StringWriter writer = new StringWriter();
        // 5.使用提供的数据模型执行模板,将生成的输出写入提供的 Writer
        template.process(map, writer);
        return writer.toString();
    }
}

最终数据展示

可以使用该json串进行数据发送调用

尊敬的张三先生,您好!您申请的尾号11111的保险合同“保单贷款”保全业务,已于2024年3月20日生效,贷款金77777元将于近期转至您尾号为8888的账户中,请您注意查收。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值