Spring boot: template not found with Freemarker

Viewed 2089

I have a spring boot application(version 2.2.5.RELEASE) which is packaged as a jar file:

I am using Freemarker(version 2.3.29) to generate an html template please find my config below:

@Configuration
public class EmailFreemarkerConfig {
     @Primary
      @Bean
        public FreeMarkerConfigurationFactoryBean getFreeMarkerConfig() {
            FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
            bean.setTemplateLoaderPath("/templates/");

            return bean;
        }

Please find my pom.xml file that included ftl file extension:

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.csv</include>
                    <include>**/*.vm</include>
                    <include>**/*.jpg</include>
                    <include>**/*.ftl</include>
                </includes>
            </resource>
        </resources>

When I packaged my jar I can find the file at the following path: BOOT-INF/classes/templates/file12.ftl

Please find below the code I am generating the html file:

   @Autowired
    private Configuration freemarkerConfig;

.....
try {
            t = freemarkerConfig.getTemplate("file12.ftl");
            html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
        } catch (IOException | TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

.... When I execute the jar from the following command:

java -jar test-batch-0.0.2-SNAPSHOT.jar

The following exception is displayed:

freemarker.template.TemplateNotFoundException: Template not found for name "file12.ftl"

When I execute same code base in integration test on eclipse it works fine.

Any idea what i am missing here please?

1 Answers

Just in case if someone else get this issue I was missig the classpath in my config:

bean.setTemplateLoaderPath("classpath:/templates/");
Related