How to Solve the Issue of Embedded Tomcat Not Serving Static Content

Question

What are the steps to troubleshoot why embedded Tomcat is not serving static content?

<dependency>
    <groupId>org.apache.tomcat_embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>9.0.43</version>
</dependency>

Answer

When using embedded Tomcat, it may not serve static content due to misconfiguration of resource handling or incorrect file paths. This guide outlines how to resolve these issues effectively.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

    @Bean
    public WebMvcConfigurer configurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
            }
        };
    }
}

Causes

  • Static resources are not properly mapped in the application's configuration.
  • Incorrect directory structure for static content.
  • The application does not include the necessary dependencies for static file serving.

Solutions

  • Ensure that the resource handler in your application is correctly configured to serve static files.
  • Verify that your static content is structured correctly in the project directory, typically under 'src/main/resources/static' for Spring Boot applications.
  • Check and add necessary dependencies for serving static content, such as 'tomcat-embed-core' in your build configuration.

Common Mistakes

Mistake: Static files are placed outside the designated static resource folder.

Solution: Ensure all static assets (CSS, JS, images) are located under 'src/main/resources/static'.

Mistake: Missing or incorrect configuration in the Java app for resource mapping.

Solution: Check your application configuration files or annotations for correct resource mappings.

Helpers

  • Embedded Tomcat
  • Static content not served
  • Spring Boot Tomcat
  • Tomcat Static Resources
  • Troubleshoot Tomcat issue

Related Questions

⦿How to Work with Date and Time in Java

Learn how to effectively manage date and time in Java with clear explanations and practical code examples.

⦿How to Perform AES Encryption and Decryption Using Bouncy Castle Provider in Java?

Learn how to encrypt and decrypt data using AES with the Bouncy Castle provider in Java. Stepbystep guide with code snippets.

⦿How to Resolve the 'Serial Port Already in Use' Error with RxTx on Mac

Discover solutions to the Serial Port Already in Use error on Mac when using RxTx. Learn troubleshooting tips and best practices.

⦿How to Copy Resources from a Dependent JAR to the Target Folder in Maven?

Learn how to copy resources from dependent JAR files to your target folder using Maven in this detailed guide.

⦿How to Compare Strings in Java Using equalsIgnoreCase?

Learn how to effectively use equalsIgnoreCase method in Java for caseinsensitive string comparison. Understand common mistakes and solutions.

⦿How to Correctly Divide Two Numbers in Programming?

Learn how to fix issues with dividing two numbers in your code including common mistakes and solutions.

⦿How to Create a Generic Method Without Parameters in Java

Learn how to define and use a generic method without parameters in Java including examples and common pitfalls.

⦿How to Retrieve MySQL Database Schema Names Using Java JDBC

Learn how to list MySQL database schema names in Java using JDBC with detailed steps code snippets and common pitfalls.

⦿What is the Most Efficient Method to Populate an ArrayList in Java?

Discover the best techniques to efficiently fill an ArrayList in Java. Learn tips tricks and common mistakes to avoid.

⦿Does Java's Garbage Collector Manage Variables Declared Inside Loops?

Explore how Javas garbage collector handles variables defined in loops and memory management techniques in Java programming.

© Copyright 2025 - CodingTechRoom.com