Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot not finding template when deployed on docker

My app works just fine if I run the application on my host using the

mvn spring-boot:run

but when I deploy it on docker, it does not work and I get this error:

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/store/index", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/store/index", template might not exist or might not be accessible by any of the configured Template Resolvers

but if I go on the url: http:localhost:8080/login which is controlled by spring security, renders the template normally. Seems to be a permission problem but I'm not sure.

here is my Dockerfile:

FROM openjdk
VOLUME /tmp
RUN touch engdevcommerce.jar
COPY target/engdevcommerce.jar engdevcommerce.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/engdevcommerce.jar"]

Solution: Turns out the problem I was having, had nothing to do with the docker deployment. I ran the jar file on my host, and I was getting the same error. The problem was that, where I returned the view url at the controllers methods, I was starting with slash like this: "/.../..." . And spring does not load view with double slash when the application is packed as .jar file. I had to remove the slash character at the beginning of the url every where I returned a ModelAndView and at the th:insert tags too on my html files.

this link helped me a lot : spring-boot-thymeleaf-not-resolving-fragments-after-packaging

like image 650
viniciusalvess Avatar asked Sep 17 '26 22:09

viniciusalvess


1 Answers

Always check the application execution first with 'java -jar your_app_name.jar' command! In general this issue is mostly resolved by checking following 3 points-

  1. Your application.properties should have the entry- spring.thymeleaf.prefix=classpath:/templates/
  2. Your controller should return name of the template without any preceding slash. As the thymeleaf configuration is case sensitive.
  3. Your Project should follow standard spring boot thymeleaf directory structure. If not, then make changes accordingly in your application.properties file.
like image 91
Caffeine Coder Avatar answered Sep 19 '26 18:09

Caffeine Coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!