Java StreamingTemplateEngine在编译groovy代码后停止工作
我将获得以下代码,以使用模板创建设置文件:Java StreamingTemplateEngine在编译groovy代码后停止工作,java,groovy,Java,Groovy,我将获得以下代码,以使用模板创建设置文件: def config_template = this.class.getResource('/config.txt.template') def template = new groovy.text.StreamingTemplateEngine(config_template) def template_data = [ data1: [ datadict1: this.class.getResource('file1.xml'),
def config_template = this.class.getResource('/config.txt.template')
def template = new groovy.text.StreamingTemplateEngine(config_template)
def template_data = [
data1: [
datadict1: this.class.getResource('file1.xml'),
datadict2: this.class.getResource('file2.xml'),
cert: this.class.getResource('cert1.jks'),
],
data2: [
datadict1: this.class.getResource('file3.xml'),
datadict2: this.class.getResource('file4.xml'),
cert: this.class.getResource('cert2.jks'),
]
String sessions_config_text = template.make(template_data)
File sessions_config = new File().with { f ->
f.withWriter('UTF-8') { w ->
w.write(sessions_config_text)
}
return f
}
该代码被编译并打包到jar文件中。当我运行它时,我得到:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:745)
Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.text.StreamingTemplateEngine.make() is applicable for argument types: (java.util.LinkedHashMap) values: [[data1:[datadict1:null, datadict2:null, cert:null, ...]]
Possible solutions: wait(), any(), wait(long), use([Ljava.lang.Object;), each(groovy.lang.Closure), any(groovy.lang.Closure)
这让我想到StreamingTemplateEngine.make()
将只接受groovy的Map
类型,但是当代码编译成java时,Map
将变成LinkedHashMap
并且make()
方法签名将无效
是这样吗 如果是这样,如何使模板引擎工作
如果没有,我做错了什么?如何修复它?首先,您需要创建with方法的实例。然后您可以调用创建的模板。我意识到问题所在,这只是一个简单的错误销售。谢谢你的回答。@sargas那么你的打字错误与答案有关吗?这个问题的未来访问者可能会被问到实际解决方案中。@cfrick否。我调用了
create\u template
方法,而不是createTemplate
。所以,我认为对来访的人没有好处。