Spring integration 使用freemarker动态生成和格式化模板
我的目标是使用free marker或任何其他可以巧妙实现的方法将java map集合格式化为字符串(基本上是csv)。我想使用存储在数据库中并由管理应用程序管理的配置数据生成模板。 配置将告诉我给定数据(哈希映射中的键)需要转到什么位置,以及在将其应用到给定位置之前是否需要对该数据运行任何脚本。如果数据不在地图中,则多个位置可能为空。 我正在考虑使用免费标记来构建这个通用工具,如果您能分享我应该如何做,我将不胜感激Spring integration 使用freemarker动态生成和格式化模板,spring-integration,freemarker,Spring Integration,Freemarker,我的目标是使用free marker或任何其他可以巧妙实现的方法将java map集合格式化为字符串(基本上是csv)。我想使用存储在数据库中并由管理应用程序管理的配置数据生成模板。 配置将告诉我给定数据(哈希映射中的键)需要转到什么位置,以及在将其应用到给定位置之前是否需要对该数据运行任何脚本。如果数据不在地图中,则多个位置可能为空。 我正在考虑使用免费标记来构建这个通用工具,如果您能分享我应该如何做,我将不胜感激 我还想知道spring集成中是否有内置is支持来构建这样的过程,因为该应用程序
我还想知道spring集成中是否有内置is支持来构建这样的过程,因为该应用程序是SI应用程序。我不是freemarker专家,但快速查看一下他们的快速入门文档,我就知道了
public class FreemarkerTransformerPojo {
private final Configuration configuration;
private final Template template;
public FreemarkerTransformerPojo(String ftl) throws Exception {
this.configuration = new Configuration(Configuration.VERSION_2_3_23);
this.configuration.setDirectoryForTemplateLoading(new File("/"));
this.configuration.setDefaultEncoding("UTF-8");
this.template = this.configuration.getTemplate(ftl);
}
public String transform(Map<?, ?> map) throws Exception {
StringWriter writer = new StringWriter();
this.template.process(map, writer);
return writer.toString();
}
}
或者,您可以使用SpringIntegration的现有脚本语言(或其他受支持的脚本语言),而无需编写任何代码(脚本除外)
public class FreemarkerTransformerPojoTests {
@Test
public void test() throws Exception {
String template = System.getProperty("user.home") + "/Development/tmp/test.ftl";
OutputStream os = new FileOutputStream(new File(template));
os.write("foo=${foo}, bar=${bar}".getBytes());
os.close();
FreemarkerTransformerPojo transformer = new FreemarkerTransformerPojo(template);
Map<String, String> map = new HashMap<String, String>();
map.put("foo", "baz");
map.put("bar", "qux");
String result = transformer.transform(map);
assertEquals("foo=baz, bar=qux", result);
}
}
<int:transformer ... ref="fmTransformer" method="transform" />