Serving Static Web Content with Spring Boot

Engineering | Roy Clarkson | December 19, 2013 | ...

We made a few announcements recently about the Spring getting started guides, including that the catalog of guides have been migrated to Asciidoctor. We also added several new client-side guides illustrating how to connect to Spring services from a variety of client technologies.

In this post I want to highlight an interesting capability of Spring Boot; within many of the client-side guides we utilized Spring Boot to stand up a Tomcat instance and serve static content. In these guides we are demonstrating JavaScript client code, not Java or Groovy! If you are already familiar with Boot, then you can probably guess the punchline. To accomplish this, there is no configuration, and almost no server code required.

Using the Spring Boot command line interface and a minimal amount of Groovy code you can direct Spring Boot to start Tomcat. Consider the following app.groovy file:

@Controller class JsApp { }

The single empty class, annotated with @Controller, will trigger AutoConfiguration within Boot, which will set up a full Spring MVC stack and enable support for Tomcat.

You can run the app using the following command:

$ spring run app.groovy

While this may not be a new revelation to those of you that have been following Spring Boot since the SpringOne announcement, there is one detail for which you may not be aware. Spring Boot will automatically add static web resources located within any of the following directories:

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

In the case of the Consuming a RESTful Web Service with jQuery guide, we included index.html and hello.js files in the /public/ folder. This means that not only does Spring Boot offer a simple approach to building Java or Groovy apps, you can also use it to easily deploy client-side JavaScript code and test it within a real web server environment!

You can analyze the details of how this works by reviewing the source code for WebMvcAutoConfiguration in Spring Boot, you will see the following declaration for a string array containing locations for Classpath resources.

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
		"classpath:/META-INF/resources/", "classpath:/resources/",
		"classpath:/static/", "classpath:/public/" };

Further in the code you can see that these locations are added to a Spring MVC ResourceHandlerRegistry.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
	if (!registry.hasMappingForPattern("/webjars/**")) {
		registry.addResourceHandler("/webjars/**").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
	}
	if (!registry.hasMappingForPattern("/**")) {
		registry.addResourceHandler("/**").addResourceLocations(
				RESOURCE_LOCATIONS);
	}
}

To review, web AutoConfiguration is executed when Spring Boot identifies a class with the @Controller annotation. The result is that you can place static web resources in any of these locations, and those static assets are then served by Tomcat when you access your application.

Spring Boot offers many exciting features to help easily create Spring based applications that you can "just run". The fun byproduct is that it is equally as easy to build and test client-side JavaScript apps with Spring Boot.

Get the Spring newsletter

Stay connected with the Spring newsletter

Subscribe

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Tanzu Spring offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all