Tomcat 9 - Getting 404 scenarios for static resources when using Spring Boot with embedded Tomcat
Could someone explain Hey everyone, I'm running into an issue that's driving me crazy... Hey everyone, I'm running into an issue that's driving me crazy. I'm currently working on a Spring Boot application that uses embedded Tomcat (version 9.0.54). Everything is functioning well, but I'm working with a frustrating scenario when trying to serve static resources like CSS and JS files. Despite placing them in the standard locations (`src/main/resources/static` and `src/main/resources/public`), I'm getting a 404 behavior when attempting to access them through the browser. Here's my directory structure for clarity: ``` my-spring-boot-app/ āāā src/ ā āāā main/ ā āāā java/ ā āāā resources/ ā āāā static/ ā ā āāā css/ ā ā ā āāā styles.css ā ā āāā js/ ā ā āāā script.js ā āāā public/ ā āāā index.html āāā pom.xml ``` When I try to access the CSS file at `http://localhost:8080/css/styles.css`, I get a 404 behavior. The same happens for the JS file. I've tried to explicitly set the resource handler in my `WebMvcConfigurer` to make sure Spring Boot is aware of these static resources, although I thought it should be automatic: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/"); registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/"); } } ``` This has not solved the question, and Iām still unable to load the static resources. I've also checked the `application.properties` to ensure that nothing is overriding the default resource handling. Additionally, I can confirm that the embedded Tomcat server is running correctly and serving the application without issues. Does anyone have an idea of what might be causing these static resources to not be found? I've also ensured that all dependencies in my `pom.xml` are up to date and compatible, and Iām using Spring Boot version 2.5.4. Any insights would be greatly appreciated! My development environment is macOS. Thanks in advance! Has anyone else encountered this? My development environment is macOS. Has anyone dealt with something similar? I'm on Linux using the latest version of Java. Any pointers in the right direction?