Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Spring's PathMatchingResourcePatternResolver match "*" with nothing?

Tags:

spring

I am trying to get a properties file from within a zip file. I need to use a wild card, because I will want to match either "my.properties" or "my_en.properties". I create a ResourcePatternResolver like so:

ClassLoader loader = MyClass.class.getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(loader);

I succeed when I try to retrieve the "my.properties" file using no wild card:

resolver.getResource("file:C:/somePath/a.zip/META-INF/my.properties").exists()

returns true. However, if I add a wild card to the file name, it fails, e.g.,

resolver.getResource("file:C:/somePath/a.zip/META-INF/my*.properties").exists()

returns false. What can I do to match and retrieve either file? I am trying to do this in a webapp within Tomcat.

like image 382
kc2001 Avatar asked Feb 13 '26 18:02

kc2001


1 Answers

The documentation is unclear about this, but the getResource method does not use PathMatcher internally to resolve the resource (that means that no wildcard is allowed), try getResources(String locationPattern) instead.

For example :

Resource[] resources = resolver.getResources("file:C:/somePath/a.zip/META-INF/my*.properties");

for(Resource resource : resources) {
  // do something for each resource found
}
like image 74
Sébastien Helbert Avatar answered Feb 15 '26 12:02

Sébastien Helbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!