I have a problem with VelocityEngine in Spring, my task is retrieve a mail template.
This is my configuration:
SPRING-CONTEXT.xml:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=file
file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path=${template.path}
file.resource.loader.cache=${template.cache}
file.resource.loader.modificationCheckInterval=${template.modificationCheckInterval}
</value>
</property>
</bean>
JAVA:
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private VelocityEngine velocityEngine;
public void sendEMailFromTemplate(String to, String subject, String template, Object object) {
MimeMessagePreparator mmp = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setFrom(mailFrom);
message.setTo(to);
message.setSubject(subject);
Map<String,Object> model = new HashMap<String,Object>();
model.put("object", object);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, StandardCharsets.UTF_8.name(), model);
message.setText(text, true);
}
};
this.javaMailSender.send(mmp);
}
Below the error I got when the app executes the method "send":
org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler handleUncaughtException - Unexpected error occurred invoking async method 'public void test.EMailService.sendEMailFromTemplate(java.lang.String,java.lang.String,java.lang.String,java.lang.Object)'.
org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'email.vm'
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:371)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:350)
at test.EMailService.sendEMailFromTemplate(EMailService.java:44)
at test.EMailService$$FastClassBySpringCGLIB$$f6dc0165.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:108)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'email.vm'
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:343)
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:71)
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplateIntoString(VelocityEngineUtils.java:112)
at test.EMailService$1.prepare(MailService.java:41)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:359)
... 9 more
What's wrong? Thanks.
The problem was the value setted in this property:
file.resource.loader.path=${template.path}
The "template.path" property was setted with a path with the "\" character separator, so Spring erased all character separators, avoiding the correct path. Replacing "\" with "/" I solved the problem.
Thank you in any case.