美文网首页java
关于spring boot2.0升级freemark文件后缀修改

关于spring boot2.0升级freemark文件后缀修改

作者: 左手落花 | 来源:发表于2020-09-04 16:18 被阅读0次

最近项目需要接入prometheus监控,由于架构那边是基于actuator做的监控数据采集,使用spring boot 2.2.6作为开发版本,业务系统需要统一升级spring boot2.0以上才能接入监控。
在升级过程中,原来的项目中有部分业务使用freemark作为模板引擎,统一将对应的文件放到resource/template目录下。


image.png

但是application.yml未做任何配置。在spring boot 1.5.8这个版本是可以找到该视图的。

到了spring boot 2,由于freemark默认文件后缀名修改,升级后出现如下报错:


image.png

特地去翻了一下spring-boot-autoconfigure下关于freemark的配置代码,spring boot 1.5.8下的类org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties

public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {

    public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";

    public static final String DEFAULT_PREFIX = "";

    public static final String DEFAULT_SUFFIX = ".ftlh";
......
/**
     * Comma-separated list of template paths.
     */
    private String[] templateLoaderPath = new String[] { DEFAULT_TEMPLATE_LOADER_PATH };

再看下spring boot 2.2.6下同样的类文件

public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {

    public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";

    public static final String DEFAULT_PREFIX = "";

    public static final String DEFAULT_SUFFIX = ".ftlh";
......
/**
     * Comma-separated list of template paths.
     */
    private String[] templateLoaderPath = new String[] { DEFAULT_TEMPLATE_LOADER_PATH };

发现2.2.6版本的默认后缀名改掉了。

最后,解决的版本有两种:

1 修改文件的后缀名为.ftlh
2 application.yml文件添加如下配置:

freemarker:
  #设置模板后缀名
  suffix: .ftl
  # 设置文档类型
  content-type: text/html
  # 设置页面编码格式
  charset: UTF-8
  # 设置页面缓存
  cache: false
  # 设置ftl文件路径
  template-loader-path:
    - classpath:/templates

我这边选了第二种方案,有了统一配置后,即使后续再次升级,也不会担心默认配置被修改。

相关文章

网友评论

    本文标题:关于spring boot2.0升级freemark文件后缀修改

    本文链接:https://www.haomeiwen.com/subject/gsnzsktx.html