Akshay Gupta opened SPR-13316 and commented

With Spring Boot, if I have a static file src/main/resources/public/index.html then navigating to http://\/ will redirect me to that file. However, if I have a file src/main/resources/public/subdir/index.html, then navigating to http://\/subdir/ will not redirect to /subdir/index.html. As a result loading the resource fails and I get an redirected to /error with a 404 response.

In 4.1.7 there was a small workaround. Using /subdir// would redirect me to page. However, in 4.2.0 both methods redirect me to /error.


Affects: 4.1.7, 4.2 GA

Comment From: spring-projects-issues

Stéphane Nicoll commented

I am not sure what you're trying to do. If you go to http://localhost:8080/ and your index.html is served, it's because Spring Boot configures Spring MVC to add a default request mapping on / if necessary.

There is no mapping for /subdir, that's why you get a 404 (that shows you the default error page). Your hack with the extra // actually shows you the root page (and not the one in subdir) so it's not fixing any issue as far as I can see.

If you want to server /subdir/index.html you need to add a mapping for it.

Comment From: spring-projects-issues

Akshay Gupta commented

By default Boot will serve /subdir/index.html as well if I give the full path. It's the mapping between / and /index.html for a sub-directory under the public folder that doesn't work right now. So requesting /subdir/ (with the trailing slash) gives a 404 response. Should I add a mapping in such scenario as well? I expected MVC to resolve it since it does the mapping between / and /index.html for the root.

Comment From: spring-projects-issues

Stéphane Nicoll commented

Well of course it will do that since the file exists at that location. Yes you should add a mapping. Like I said in my initial answer, "Spring Boot configures Spring MVC to add a default request mapping on / if necessary", see the code

Comment From: andrea-ligios

This is how you add the mapping: https://stackoverflow.com/a/59052438/1654265

Hope that helps

Comment From: bojanv55

Is it possible to achieve this, without specifying one line for every single path? Basically - if path is a folder, then try to fetch index.html from one level below?