I have a URL as a String and I'm looking to perform various modifications to it. Specifically, I need to modify values in the query string if they already exist, and, if they don't, to create them.
It would seem to me that there should already be an existing class that I can construct with my URL as a String that would split the URL into its constituent components and allow me to modify each component individually. In the case of the query string, it should be possible to address it as a Map, modify it, then toString the URL object to get back the modified URL.
Java has a URL class but it seems extremely limited - they query string is treated as a String, not a Map, and the only way to modify it is to call set and specify all the components of the URL instead of just setting the component I want to set. Are there better alternatives out there? I'd prefer something within the standard java/javax packages, if possible, but I recognize that as unlikely.
There is no such JDK class that I know of.
Spring Web (Maven link) provides the UriComponentsBuilder
class for building UriComponents
which
Represents an immutable collection of URI components, mapping component type to string values. Contains convenience getters for all components. Effectively similar to
java.net.URI
, but with more powerful encoding options and support for URI template variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With