Spring Boot - Serving static web contents

[Updated: Jan 5, 2017, Created: Jan 5, 2017]

By default Spring boot serves static content from one of the following locations in the classpath:

  1. /static
  2. /public
  3. /resources
  4. /META-INF/resources


For a maven project, the content of src/main/resources/ is in classpath during runtime, so we will be adding above locations there.

Let's create a web page under one of the locations listed above: src/main/resources/static/page1.html

<h2>Page 1</h2>

We don't have to add any controller for static contents.

public class WebStaticPagesExample {

    public static void main (String[] args) {
        SpringApplication app =
                  new SpringApplication(WebStaticPagesExample.class);

Run the above main class and access the page at 'http://localhost:8080/page1.html' :

Pages for other above listed location are added in the project browser below, please try them one by one.

Setting new locations

We can customize the static resource locations using spring.resources.staticLocations property. That will replace the default values with a list of new directory locations.


public class ReplaceDefaultLocationsExample {

    public static void main (String[] args) {

        SpringApplication app =
                  new SpringApplication(ReplaceDefaultLocationsExample.class);

        Properties properties = new Properties();
                          "classpath:/newLocation1/, classpath:/newLocation2/");

Please check out different ways of adding properties to a boot application.

Now add a page at new location, src/main/resources/newLocation1/Page5.html

<h2>Page 5 at newLocation1</h2>

Add a similar page at src/main/resources/newLocation2/Page6.html.


Specifying additional locations for static content

We can specify additional static locations by using WebMvcConfigurerAdapter and overriding the addResourceHandlers method.

This doesn't override the default locations.

public class AddCustomLocations {
    WebMvcConfigurer configurer () {
        return new WebMvcConfigurerAdapter() {
            public void addResourceHandlers (ResourceHandlerRegistry registry) {

    public static void main (String[] args) {

        SpringApplication app =
                  new SpringApplication(AddCustomLocations.class);

Add new page: src/main/resources/new-custom-location/page7.html

<h2>Page 7 at custom location/h2>

Access page at http://localhost:8080/pages/page7.html

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter-web 1.4.2.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
    Corresponding Spring version: 4.3.4.RELEASE
  • JDK 1.8
  • Maven 3.3.9

Boot Serving Static Pages Select All Download
  • boot-serving-static-pages
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF
            • resources
          • my-custom-location
          • newLocation1
          • newLocation2
          • public
          • resources
          • static

See Also