Overview
Since all URL
constructors are deprecated starting with Java 20, we should stop using them in order to allow Spring Framework to be built using Java 20.
In addition, the rationale for deprecating those constructors (which is more important than the actual deprecation) is applicable today.
For full details, see the Constructing instances of URL section of the URL
Javadoc. The API Note also provides valuable advice regarding correct usage of File
, Path
, URI
, and URL
.
Additional Resources
- https://inside.java/2022/11/22/heads-up/
- https://bugs.openjdk.org/browse/JDK-8293590
- https://bugs.openjdk.org/browse/JDK-8295750
Comment From: jhoeller
This turns out to be rather involved due to the differences between URI and URL rules and the restricted behavior of the java.net.URI#toURL()
method. As a first step, #29486 prepares the codebase for alternative URL construction arrangements in 6.0; a full replacement/reimplementation of URL construction rules from user-supplied Strings is likely to come in 6.1 then.
Comment From: jhoeller
With a combination of URI construction from a cleaned path and subsequent defensive URL conversion, all our tests pass. This is still rather involved but seems to be equivalent for our common purposes now.
Corresponding code is available in UrlResource(String)
, ResourceUtils.toURL(String)
and ResourceUtils.toRelativeURL
in comment blocks now, for an easy switch in 6.1 (or whenever we find it appropriate).
Comment From: sbrannen
Update: added a section with additional resources regarding changes in behavior in URL constructors beginning with Java 20.