title: Cache static files by content type in Spring Boot
body: <p>I'm trying to set up caching headers on specific static file type in Spring Boot.
  In directory src/main/resources/static there are few subdirectories with different file types:</p>
  <pre><code>src/main/resources/static/font   --&gt; *.otf
  src/main/resources/static/lib    --&gt; *.js
  src/main/resources/static/images --&gt; *.png, *.jpg
  </code></pre>
  <p>Is there a way to put cache headers by file type inside Spring configuration?</p>
  <pre><code>*.otf 365 days
  *.png 30 days
  *.jpg 7 days
  </code></pre>
  <p>Spring version is 5.2.3 and Spring Boot 2.2.4 - is there a chance that Spring Boot deals with it and makes it not work?</p>
  <p>Tried with</p>
  <pre><code>@Override
  public void addResourceHandlers(final ResourceHandlerRegistry registry) {
      final CacheControl oneYearPublic =
          CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic();
      // it does not &quot;work&quot; with &quot;/static/fonts/&quot;
      registry
          .addResourceHandler(&quot;/fonts/{filename:\\w+\\.otf}&quot;)
          .setCacheControl(oneYearPublic);
  }
  </code></pre>
  <p>but I get weird results. When checking with Network tab of DevTools I get these headers:</p>
  <pre><code>    Cache-Control: no-cache, no-store, max-age=0, must-revalidate
      Pragma: no-cache
      Expires: 0
  </code></pre>
  <p>But when I go to the URL directly, I get 404</p>
  <pre><code>http://localhost/fonts/1952RHEINMETALL.otf
  </code></pre>
  <p>Without any configuration I get &quot;no-store&quot; Cache-Control header.</p>