Align UriComponents.toUri() with toUriString()

Update HierarchicalUriComponents.toUri() to only prepend a missing '/'
when the scheme, user info, host or port are specified. This makes
the toUri() method behave in the same way as .toUriString() and allows
relative URIs to be created.

Issue: SPR-10231
This commit is contained in:
Phillip Webb
2013-02-11 11:17:23 -08:00
parent 203b22b246
commit 2ca75386f1
2 changed files with 19 additions and 2 deletions

View File

@@ -407,7 +407,10 @@ final class HierarchicalUriComponents extends UriComponents {
else {
String path = getPath();
if (StringUtils.hasLength(path) && path.charAt(0) != PATH_DELIMITER) {
path = PATH_DELIMITER + path;
// Only prefix the path delimiter if something exists before it
if(getScheme() != null || getUserInfo() != null || getHost() != null || getPort() != -1) {
path = PATH_DELIMITER + path;
}
}
return new URI(getScheme(), getUserInfo(), getHost(), getPort(), path, getQuery(),
getFragment());