Spring Boot project with static content generates 404 when running jar Spring Boot project with static content generates 404 when running jar spring spring

Spring Boot project with static content generates 404 when running jar


It turns out that whilst Spring Boot is being clever at adding the various resource directories to the classpath, Maven is not and it's up to you to deal with that part. By default, only src/main/resources will be included in your JAR. If you create a folder called /static in the root of your project (as implied by the blog post) then it will work fine whilst using the spring-boot:run Maven goal but not once you create a JAR.

The easiest solution is to create your /static folder inside /src/main/resources and then Maven will include it in the JAR. Alternative you can add additional resource locations to your Maven project:

<resources>    <resource>        <directory>src/main/resources</directory>    </resource>    <resource>        <directory>static</directory>        <targetPath>static</targetPath>    </resource></resources>

I hope this is useful to someone, it's kind of obvious when you step back and look at how Maven works but it might stump a few people using Spring Boot as it's designed to be pretty much configuration free.


I am banging my head against the wall trying to figure out how to do this with gradle. Any tips?

EDIT: I got it to work by adding this to my build.gradle:

// Copy resources into the jar as static content, where Spring expects it.jar.into('static') {    from('src/main/webapp')}


I was going around few pages to understand how to serve static content in Spring boot environment. Mostly all advises was around placing the static files with in /static /resources/ src/main/webapp etc. Thought of sharing below approach.

  1. Allow spring boot to auto configure Dispatcher Servlet - Make sure DispatcherServletAutoConfiguration is not in the exclude for AutoConfiguration.

    @EnableAutoConfiguration(exclude = { //DispatcherServletAutoConfiguration.class, })

  2. Inject your external directory for static content routing

    @Value("${static-content.locations:file:C:/myprj/static/")private String[] staticContentLocations;

3.Override WebMvcAutoConfiguration using WebMvcConfigurerAdapter to advice spring not to use default resource Location but use what we instruct it.Like below

@Bean    public WebMvcConfigurerAdapter webMvcConfigurerAdapter()    {        return new WebMvcConfigurerAdapter()        {            @Override            public void addResourceHandlers(ResourceHandlerRegistry registry)            {                if (!registry.hasMappingForPattern("/**"))                {                    // if this is executed spring won't add default resource                    // locations - add them to the staticContentLocations if                    // you want to keep them                    // default locations:                    // WebMvcAutoConfiguration.RESOURCE_LOCATIONS                    registry.addResourceHandler("/**").addResourceLocations(                            staticContentLocations);                }            }        };    }

If C:/myprj/static has index.html , then http://localhost:portno/index.html should work. Hope that helps.