Skip to content

Avoid use of java.net.URL constructors #29481

Closed

Description

@sbrannen

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

Activity

  1. added
    in: coreIssues in core modules (aop, beans, core, context, expression)
    on Nov 14, 2022
  2. modified the milestones: 6.0.x, 6.0.0 on Nov 14, 2022
  3. modified the milestones: 6.0.0, 6.1.x on Nov 14, 2022
  4. jhoeller commented on Nov 14, 2022

    @jhoeller
    Contributor

    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.

  5. jhoeller commented on Nov 14, 2022

    @jhoeller
    Contributor

    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).

  6. sbrannen commented on Nov 28, 2022

    @sbrannen
    MemberAuthor

    Update: added a section with additional resources regarding changes in behavior in URL constructors beginning with Java 20.

  7. modified the milestones: 6.1.x, 6.1.0-M1 on Jan 10, 2023
  8. added a commit that references this issue on Sep 9, 2024
    9342317
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions