We will try to quickly how to go about loading and serving static content using Spring Boot 2. There was a difference in how boot 2.0.0 was loading static resources to 2.1 version now. In this article, we will take a simple web application and load the static resources that are defined in the resource folder.
Here is how the project is setup for this article.
I decoupled the pom as a parent and one for the web project. Spring by default without any config changes loads static content from the path that is defined in ResourceWebHandler.
But in the article we are going to overwrite the default path.
We first over-ride the classWebMVCConfigurer
. The sample snippet of override is.,
public class BootSamplerWebAppWebConfigurer implements WebMvcConfigurer{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/", "/error/","/images"));
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/")
.setViewName("/main/index.html");
}
}
As we can see, there are the class couple of methods that we override to tell boot where our static resources are located. I don’t like the favicon that comes from spring framework. So here is the code to serve favicon static content. Here is the code to override that.
@Configuration
public class BootSamplerFavIconConfiguration {
@Bean
public SimpleUrlHandlerMapping customFavIconHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
simpleUrlHandlerMapping.setOrder(Integer.MIN_VALUE);
simpleUrlHandlerMapping.setUrlMap(Collections.singletonMap("/favicon.ico", faviconRequestHandler()));
return simpleUrlHandlerMapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler resourceHttpRequestHandler = new ResourceHttpRequestHandler();
ClassPathResource classPathResource = new ClassPathResource("/static/images/favicon.ico");
List<Resource> locations = Arrays.asList(classPathResource);
resourceHttpRequestHandler.setLocations(locations);
return resourceHttpRequestHandler;
}
}
Error pages are from a static content directory, we will have to override the class ErrorrPageRegistar
public class BootSamplerWebAppErrorRegistrar implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html"));
registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400.html"));
}
}
The main method to run the application has some tweaks, here is the code block for that.
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class})
public class MainApp extends SpringBootServletInitializer {
public static void main(String[] args) {
final SpringApplication springApplication =
new SpringApplication(BootSamplerWebAppErrorRegistrar.class,
BootSamplerWebAppWebConfigurer.class, BootSamplerFavIconConfiguration.class,
MainApp.class);
springApplication.run(args);
}
}
When running the, we should get index.html
served from the folder main
. The template for this index is from the bootstrap template blackrock digital.
Finally all code for this is in github.