Import error templates from a external jar

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Import error templates from a external jar

Kerruba
Hello,

I've a multi module project with multiple webapps serving different purposes but sharing same look and feel.
What I'm trying to achive is extract all the error templates into a separate jar file and include them as a maven dependency in each webapp.

What I've done so fare is creating a new module, define a main class with SpringBootApplication annotation, and defined multiple templates within the resources/templates/error folder.
I'm then adding this module as a dependency in the pom file of another webapp and run it as a war in Tomcat.
When I try to reach generate an error, Spring is not reading the custom error templates but generates the default whitelabel error page.

The next step in my test was to create a controller in my main webapp and reference from there a test template created in the inherited jar. This is working.
Another test I've done is to define an request mapping in my jar and try to reach that from the webapp, and that's working too.

I've tried to create also a TemplateEngine Bean with a template resolver that point to the jar, but maybe I'm doing something wrong...
Here the code for the TemplateEngine and the TemplateResolver that I've created
@Bean
    public TemplateEngine errorTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(errorTemplateResolver());
        return templateEngine;
    }

    private ITemplateResolver errorTemplateResolver() {
        final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setOrder(0);
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCacheable(false);
        return templateResolver;
    }


Could you help me?