Springboot Static resource version management

Source: Internet
Author: User

When we do web development, it is unavoidable to deal with static resources (css,js,images), the common practice is to add version information on the requested URL, so that you can take advantage of the client caching mechanism, only when the resource content changes, you need to request from the server and load the latest version of resources. Spring's static resource management Resourceurlprovider

First, increase the resource processor in MVC

@Configuration public
class Mvcapplication extends Webmvcconfigureradapter {
  @Override public
  Void Addresourcehandlers (Resourcehandlerregistry registry) {
    Versionresourceresolver versionresourceresolver = new Versionresourceresolver ()
        . Addversionstrategy (New Contentversionstrategy (), "/**");
    Registry.addresourcehandler ("/javascript/*.js")
        . Addresourcelocations ("classpath:/static/")
        . Setcacheperiod * 365)/* One year/
        Resourcechain (True)
        addresolver (versionresourceresolver); c13/>}
  ...
}

In Springboot, you can use the following configuration

Spring: Resources
  :
    chain:
      enabled:true
      cache:true
      version:
        content:
          enabled:true
          paths:/static/**
        fixed:
          enabled:true
          version:1.0
          paths:/js/**,/foo/**

Or

Spring: Resources
  :
    chain:
      enabled:true
      cache:true
      version.content:
        enabled:true
        paths:/static/**
      version.fixed:
        enabled:true
        version:1.0 paths
        :/js/**,/foo/**

Static resource path modify according to actual path

**CONTENTVERSIONSTRATEGY**MD5 policy, e.g., request

/javascript/test-69ea0cf3b5941340f06ea65583193168.js

is resolved to:

/javascript/test.js

fixedversionstrategy Fixed version policy, such as

/v1.2.3/javascript/test.js

Using resourceurlprovider build contains MD5 information

With a resourceurlprovider a resource url (e.g./javascript/test.js) can is converted to a versioned URL (e.g./javascript /test-69ea0cf3b5941340f06ea65583193168.js). A Resourceurlprovider Bean with the ID mvcresourceurlprovider be automatically declared with the MVC Configuratio N.

If you use the thymeleaf template engine (you can access the Resourceurlprovider Bean directly), you can use the front-end page directly:

<script type= "Application/javascript"
        th:src= ${@mvcResourceUrlProvider. Getforlookuppath ('/javascript/ Test.js ')} ' >
</script>

If you use another template engine such as Freemarker, you can add the Resourceurlprovider bean to the model attribute by @controlleradvice, just like this:

@ControllerAdvice public
class Resourceurladvice {
  @Inject
  resourceurlprovider resourceurlprovider;
  @ModelAttribute ("URLs") public
  resourceurlprovider URL () {return
    this.resourceurlprovider;
  }
}

In this case, the front-end page can be written like this

<script type= "Application/javascript" 
        th:src= "${urls.getforlookuppath ('/javascript/test.js ')}" >
</script>

This way supports all the template engines. Resourceurlencodingfilter

Another way is by Resourceurlencodingfilter, which is the servlet Filter, by overriding Httpservletresponse.encodeurl () to generate a versioned resource address. It is also simple to use, adding resourceurlencodingfilter beans to the configuration item as follows:

@SpringBootApplication public
class Mvcapplication extends Webmvcconfigureradapter {
  @Override
  public void Addresourcehandlers (Resourcehandlerregistry registry) {
    //same as before.
  }
  @Bean public
  Resourceurlencodingfilter Resourceurlencodingfilter () {return
    new Resourceurlencodingfilter () ;
  }
  ...
}

If you are using a template engine (such as: JSPs, Thymeleaf, Freemarker and Velocity) to support direct call to the Response.encodeurl () method, the version information is automatically added to the resource URL.

For example, in Thymeleaf, we can use @{.} Syntax

<script type= "Application/javascript" th:src= "@{/javascript/test.js}" ></script>

Then the final page will be as follows:

<script type= "Application/javascript" 
        src= "/javascript/test-69ea0cf3b5941340f06ea65583193168.js" >
</script>

Springboot 1.3.0 version, the use of * resourceurlencodingfilter* will be simpler

As of the Spring Boot 1.3.0, this are now even easier. The Resource chain can be configured via Boot properties and the Resourceurlencodingfilter is automatically registered


Links to are rewritten at runtime on template, to a resourceurlencodingfilter, auto-configured for Thymel Eaf and Velocity. You are should manually declare this filter when using JSPs. Template engines aren ' t automatically supported right now, but can is with custom template macros/helpers and the US E of the Resourceurlprovider.

Https://github.com/spring-projects/spring-boot/pull/3123

Https://dzone.com/articles/2-step-resource-versioning-with-spring-mvc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.