title: Serving static files with spaces in file names with Spring Boot
body: <p>I've found a very strange behavior in Spring Boot when trying to serve static files with spaces (or any other special chars, like accents) in file names.</p>
  <p>I'm using Spring Boot 2.6.1 with Spring Web MVC and the following customization:</p>
  <pre class="lang-java prettyprint-override"><code>@Configuration
  public class MyConfig implements WebMvcConfigurer {
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler(&quot;/repo/**&quot;)
                  .addResourceLocations(&quot;file:///srv/intranet/repo/&quot;);
      }
  }
  </code></pre>
  <p>I have two files under <code>/srv/intranet/repo</code>, named <code>foo.txt</code> and <code>foo bar.txt</code> (note the space in the second file name).</p>
  <p>When I start my application, I can access the first file at <code>http://localhost:8080/repo/foo.txt</code>. But I cannot access the second file (the one with the space in the file name) at <code>http://localhost:8080/repo/foo%20bar.txt</code> (I get a 404 error).</p>
  <p>BUT if I put the file <code>foo bar.txt</code> under <code>src/main/resources/static</code>, then I can acces the file at <code>http://localhost:8080/foo%20bar.txt</code>.</p>
  <p>I'm aware that Spring Boot <a href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.servlet.spring-mvc.static-content" rel="nofollow noreferrer">configures several directories</a> by default to serve static content (one of them being <code>classpath:/static/</code>), so I'm wondering: what is the difference between the preconfigured directories and the one I'm adding in my @Configuration class via <code>addResourceHandler().addResourceLocations()</code>? Am I missing some details when adding the new resourceHandler?</p>
  <p><strong>WORKAROUND</strong></p>
  <p>You can set the following property in your <code>application.properties</code> (or equivalent .yml) to get the old behavior in Spring Boot (pre-v2.6.0):</p>
  <pre><code>spring.mvc.pathmatch.matching-strategy=ant-path-matcher
  </code></pre>
  <p><strong>UPDATE</strong></p>
  <p>I believe this is probably a bug in <code>PathPattern</code>, which replaces <code>AntPathMatcher</code>, and was introduced in Spring Framework 5.3 and adopted in Spring Boot 2.6.0. <a href="https://github.com/spring-projects/spring-boot/issues/28939" rel="nofollow noreferrer">I submitted a bug report</a>.</p>
  <p><strong>UPDATE (2022-06-04)</strong></p>
  <p>The bug has been <a href="https://github.com/spring-projects/spring-framework/issues/27791" rel="nofollow noreferrer">fixed</a>. The fix will be included in Spring Framework 5.3.21.</p>