Backend Development 3 min read

Resolving Thymeleaf Template Not Found Errors in Spring Boot 3.4.3 When Running as a JAR

After encountering Thymeleaf template parsing failures when packaging a Spring Boot 3.4.3 application as a JAR, the author resolves the issue by configuring the template resolver in Java code, disabling Thymeleaf settings in application.yml, and adjusting ModelAndView paths to omit leading slashes.

Java Captain
Java Captain
Java Captain
Resolving Thymeleaf Template Not Found Errors in Spring Boot 3.4.3 When Running as a JAR

The author revisits Spring Boot configuration to provide a Thymeleaf template engine for a small project, noting that while development environments (Eclipse, IDEA, VSCode) work fine, the packaged JAR fails to locate template resources.

Environment details: Spring Boot version 3.4.3, Java JDK 17. The failure manifests as org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [static//catch/catch-index.html]") .

To address the issue, the author defines the template resolver and engine directly in Java configuration, avoiding reliance on application.yml settings that may conflict when WebMvcConfigurer is implemented.

/** * Thymeleaf template resolver * @return */ @Bean public ITemplateResolver templateResolver() { SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); resolver.setPrefix("classpath:/static/"); resolver.setSuffix(".html"); resolver.setTemplateMode("HTML"); resolver.setCharacterEncoding("UTF-8"); resolver.setCacheable(false); return resolver; } /** * Spring template engine * @param templateResolver * @return */ @Bean public TemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; }

The author also comments out Thymeleaf-related sections in application.yml to prevent conflicting configurations.

When returning a ModelAndView , the path should not start with a leading slash. The corrected controller method is:

@RequestMapping("/catch") public ModelAndView catch(@RequestParam(value="code", required=false) String code) { //ModelAndView mv = new ModelAndView("/catch/catch-index.html"); ModelAndView mv = new ModelAndView("catch/catch-index.html"); return mv; }

With these adjustments, the application runs successfully from the JAR, and the template is correctly resolved.

Key takeaways: 1) If WebMvcConfigurer is implemented, avoid configuring Thymeleaf in application.yml . 2) In ModelAndView , omit the leading slash in the view name.

Javabackend developmentSpringBootThymeleafmodelandviewtemplate-resolver
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.