Retain original URL instance in case of custom URLStreamHandler

Closes gh-33199
This commit is contained in:
Juergen Hoeller 2024-07-11 16:15:42 +02:00
parent 1880e109c3
commit 2bfff7fc37
1 changed files with 7 additions and 1 deletions

View File

@ -428,12 +428,18 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
if (!cleanedPath.equals(urlString)) {
// Prefer cleaned URL, aligned with UrlResource#createRelative(String)
try {
return new UrlResource(ResourceUtils.toURI(cleanedPath));
// Cannot test for URLStreamHandler directly: URL equality for same String
// in order to find out whether original URL uses default URLStreamHandler.
if (ResourceUtils.toURL(urlString).equals(url)) {
// Plain URL with default URLStreamHandler -> replace with cleaned path.
return new UrlResource(ResourceUtils.toURI(cleanedPath));
}
}
catch (URISyntaxException | MalformedURLException ex) {
// Fallback to regular URL construction below...
}
}
// Retain original URL instance, potentially including custom URLStreamHandler.
return new UrlResource(url);
}
}