Issue
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.
I'm using Spring Boot 2.6.1 with Spring Web MVC and the following customization:
@Configuration
public class MyConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/repo/**")
.addResourceLocations("file:///srv/intranet/repo/");
}
}
I have two files under /srv/intranet/repo
, named foo.txt
and foo bar.txt
(note the space in the second file name).
When I start my application, I can access the first file at http://localhost:8080/repo/foo.txt
. But I cannot access the second file (the one with the space in the file name) at http://localhost:8080/repo/foo%20bar.txt
(I get a 404 error).
BUT if I put the file foo bar.txt
under src/main/resources/static
, then I can acces the file at http://localhost:8080/foo%20bar.txt
.
I'm aware that Spring Boot configures several directories by default to serve static content (one of them being classpath:/static/
), so I'm wondering: what is the difference between the preconfigured directories and the one I'm adding in my @Configuration class via addResourceHandler().addResourceLocations()
? Am I missing some details when adding the new resourceHandler?
WORKAROUND
You can set the following property in your application.properties
(or equivalent .yml) to get the old behavior in Spring Boot (pre-v2.6.0):
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
UPDATE
I believe this is probably a bug in PathPattern
, which replaces AntPathMatcher
, and was introduced in Spring Framework 5.3 and adopted in Spring Boot 2.6.0. I submitted a bug report.
Solution
I have found a workaround for this issue.
Just add the following in your Spring Boot configuration file application.properties
:
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
The documentation for this property states that ant-path-matcher
is the default value, but it is not. The source code shows that the default value is path-pattern-parser
. I submitted an issue.
Answered By - aaguilera
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.