spring.resources.static-locations not working with devtools

2 min read 07-10-2024
spring.resources.static-locations not working with devtools


Spring Boot Devtools: Static Resources and the "Resource Not Found" Dilemma

Spring Boot's Devtools are a developer's best friend, providing instant restarts and live code reloading. However, when it comes to serving static resources, they can sometimes create a frustrating "Resource Not Found" issue. This article will dissect the problem, explain the underlying cause, and present solutions to get your static resources working seamlessly with Devtools.

The Scenario:

Imagine you have a Spring Boot application with a resources directory containing your CSS, JavaScript, and image files. You've configured your application.properties file to point to this directory:

spring.resources.static-locations=classpath:/resources/

Everything works fine when you run your application in a regular production environment. But when you enable Devtools, you find that your static resources are not being served correctly. Browsers are throwing "404 Not Found" errors, despite the resources being present in the resources folder.

The Root of the Problem:

Devtools uses an internal web server (usually Tomcat) to provide the live reloading functionality. This server operates in a different context than your main application's web server (which could be Tomcat, Jetty, or Undertow). This difference in context leads to a mismatch in the way static resources are accessed. When you enable Devtools, the internal server doesn't find the resources at the location specified in spring.resources.static-locations.

Solutions to the Resource Conflict:

  1. Using Spring Boot's spring.resources.static-locations with the classpath prefix:

    The most common solution is to use classpath: as the prefix in your static location configuration. Devtools can then access resources from the classpath without any conflicts.

    spring.resources.static-locations=classpath:/resources/
    
  2. Including Resources in the META-INF/resources Directory:

    Spring Boot automatically serves resources placed in the META-INF/resources directory. This is a standard location for web application resources, so Devtools can find them without issues.

    # Create the directory if it doesn't exist
    src/main/resources/META-INF/resources/
    
    # Move your static resources into this directory
    
  3. Setting the spring.devtools.restart.exclude Property:

    This solution allows you to exclude specific directories from the restart process, ensuring that your static resources are not affected. This approach is useful if you have separate directories for your static resources and your application code.

    spring.devtools.restart.exclude=resources/**
    

Important Note:

  • Using classpath: in spring.resources.static-locations is generally the simplest and most recommended approach for ensuring compatibility with Devtools.
  • If you're still encountering issues, check your build configuration to ensure that static resources are correctly packaged and included in your application's classpath.

Additional Value:

This article provided a comprehensive overview of the common "Resource Not Found" issue when using Devtools. It offered three practical solutions, each with its own advantages and considerations. By understanding the underlying cause of the conflict and implementing these solutions, developers can ensure their static resources are served correctly, even when utilizing Devtools' powerful features.

Resources:

By implementing these solutions and understanding the underlying causes, developers can achieve a seamless workflow, enjoying the benefits of Devtools without sacrificing the proper serving of static resources.