Provided I have a java.net.URL object, pointing to let's say
http://example.com/myItems
or http://example.com/myItems/
Is there some helper somewhere to append some relative URL to this?
For instance append ./myItemId
or myItemId
to get :
http://example.com/myItems/myItemId
You can use URIBuilder and the method
URI#normalize
to avoid duplicate/
in the URI:UPDATED
I believe this is the shortest solution:
Some examples using the Apache URIBuilder http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html:
Ex1:
Result 1 -> http://example.com/test/example
Ex2:
Result 2 -> http://example.com/test/example
Concatenate a relative path to a URI:
res
will containhttps://stackoverflow.com/questions/some/path
You can just use the
URI
class for this:Note the trailing slash on the base path and the base-relative format of the segment that's being appended. You can also use the
URIBuilder
class from Apache HTTP client:...
URL
has a constructor that takes a baseURL
and aString
spec.Alternatively,
java.net.URI
adheres more closely to the standards, and has aresolve
method to do the same thing. Create aURI
from yourURL
usingURL.toURI
.