Given a java.net.URI
object, I need to either:
path
component). However there are no setX
methods, so it seems these objects are supposed to be immutable.URI
object that is the same as the original except for a given field ("functional update"). However there are no withX
methods, so I would have to write my own logic to deal with this.Do I really have to write my own functions to deal with modification of URI
objects?
Yes you would have create a new object each time since the java.net.URI
is immutable. You may use a third-party class like Apache HttpComponents' URIBuilder.
Example from the official tutorial:
URI uri = new URIBuilder()
.setScheme("http")
.setHost("www.google.com")
.setPath("/search")
.setParameter("q", "httpclient")
.setParameter("btnG", "Google Search")
.setParameter("aq", "f")
.setParameter("oq", "")
.build(); // the build method creates a new URI instance behind the scenes
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