我使用FreeMarker作为构建字符串的一种方式,这是作为对REST请求的响应放置在线上的POJO的属性。我已经将Freemarker添加到项目的Maven pom中,我可以将配置注入到我的服务中,但为了让类加载器找到我的模板,我必须做一件特别的事情。Spring Boot + FreeMarker + RestController加载模板
private Template getTemplate() throws Exception {
configuration.setClassForTemplateLoading(this.getClass(), "/template/");
Template temp = configuration.getTemplate("test.ftl");
return temp;
}
是否有更简洁的方式来配置FreeMarker寻找模板的位置?使用@Bean
生成FreeMarkerConfigurer
完全不影响配置。
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html你可以把'spring.freemarker.template- loader-path = classpath:/ template /#逗号分隔的模板路径列表到您的应用程序属性中。 (或者坚持惯例,并使用's模板') – zapl