getResource can throw IllegalArgumentException

Class.getResource, ClassLoader.getResource, and ClassLoader.getSystemResource will throw IllegalArgumentException if a malformed URL is provided to them.

According to its javadoc, resolveURL should return null if not resolvable, so catch the IllegalArgumentException and return null.
This commit is contained in:
Craig Andrews
2021-02-20 00:28:03 -05:00
committed by Juergen Hoeller
parent e53cce0778
commit dee12db50a

View File

@@ -148,14 +148,19 @@ public class ClassPathResource extends AbstractFileResolvingResource {
*/
@Nullable
protected URL resolveURL() {
if (this.clazz != null) {
return this.clazz.getResource(this.path);
try {
if (this.clazz != null) {
return this.clazz.getResource(this.path);
}
else if (this.classLoader != null) {
return this.classLoader.getResource(this.path);
}
else {
return ClassLoader.getSystemResource(this.path);
}
}
else if (this.classLoader != null) {
return this.classLoader.getResource(this.path);
}
else {
return ClassLoader.getSystemResource(this.path);
catch (IllegalArgumentException ex) {
return null;
}
}