Spring MVC resource versioning with adding ResourceUrlEncodingFilter

I have been trying to get new versioning of resources since 4.1. Of




I cannot register the ResourceUrlEncodingFilter correctly. How do you do this so that it picks up urls in jsp? I am using javaconfig and in my extended method WebMvcConfigurerAdapter -> addResourceHandlers I have

                    new VersionResourceResolver()
                            .addFixedVersionStrategy("1.1.0", "/**/*.js")


This seems to work as I can get the changes in the controller

private ResourceUrlProvider resourceUrlProvider;

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String homePub() {

    logger.debug("js =  '{}'", this.resourceUrlProvider.getForLookupPath("/static/test.js"));
    logger.debug("css =  '{}'", this.resourceUrlProvider.getForLookupPath("/static/test.css"));

    return "test";


Will output

DEBUG TestController - js =  '/static/1.1.0/test.js'
DEBUG TestController - css = '/static/styles/test-4c517674c05348b2aa87420e7adc420b.css'


Originally urls in jsp are ignored, so I added

container.addFilter("resourceUrlEncodingFilter",  ResourceUrlEncodingFilter.class).addMappingForUrlPatterns(
            null, true, "/*");


To my implementation of WebApplicationInitializer

This gives below exception

java.lang.IllegalStateException: Failed to determine lookup path: /test/static/test.js


So at least I know what the filter is called, it just didn't take my handlers I tried to add a resource handler for / test / static / ** but it didn't help. Changing isMatchAfter to false stops the exception, but the filter doesn't seem to be called.

The jsp question is very simple

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

<c:url value='/static/test.js'/>


So my guess is not how to configure the ResourceUrlEncodingFilter, how should it be added


source to share

1 answer

There seems to be a mistake


You need to add

public HandlerMapping resourceHandlerMapping() {
    SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) super.resourceHandlerMapping();
    return handlerMapping;


And extend WebMvcConfigurationSupport instead of WebMvcConfigurerAdapter

There are other problems too


And securityFilter causes problems, but the above should contain the basics

Update: Release 4.1.2 addresses these issues



All Articles