Spring Boot app in Docker container: Could not open ServletContext resource [/WEB-INF/views/login.html]

Viewed 429

I have a Spring Boot Web App that runs correctly when I launch it from Spring Tool Suite or by java -jar app.jar

Unfortunately when I make a Docker container of it I get an error due to thymeleaf pages:

Could not open ServletContext resource [/WEB-INF/views/login.html]

I would like to specify that I know that it's not a standard (recommended) location for template pages (src/main/resources is meant to be used) but I already added the src/main/webapp folder as resource.

<resources>          
    <resource>
        <directory>src/main/webapp</directory>
    </resource>             
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

I can't understand why it works when I do java -jar app.jar but doesn't work when the same command is executed into a Docker container.

I inspected the container with Visual studio Code and unpacked the app.jar and the content is correct, under the BOOT-INF/classes I have:

WEB-INF
application.properties
templates
logback-spring.xml
...

under WEB-INF there is the view folder and under the view folder there are my static files: login.html, 500.html etc.

1 Answers

Solution posted by the OP (HK15) as an edit to the question:


I had to add the prefix "classpath" in the template resolver Bean:

 resolver.setPrefix("classpath:/WEB-INF/views/");

But honestly I don't know why this is necessary only to make it work in a Docker container.

Related