Troubleshooting spring resource handlers

If you have configure the spring resource handlers but didn’t display correctly on the browser, try check the following configuration.

Web @Configuration

This sample code show that missing line @EnabledWebMVC, therefore the resource handler is not activated.

@Configuration
@ComponentScan(basePackageClasses = Application.class)
class WebMvcConfig extends WebMvcConfigurerAdapter {

    private static final String VIEWS = "/WEB-INF/views/";
    private static final String RESOURCES_LOCATION = "/resources/";
    private static final String RESOURCES_HANDLER = RESOURCES_LOCATION + "**";

   @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
              .addResourceHandler(RESOURCES_HANDLER)
              .addResourceLocations(RESOURCES_LOCATION);
    }
    
    // code omitted

}

 

Make sure you have annotated @EnableWebMvc, refresh browser and you’ll see the result.

Screen Shot 2015-07-27 at 3.40.07 PM

Troubleshooting spring resource handlers

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.