Class UriComponentsBuilder
- All Implemented Interfaces:
Cloneable
,UriBuilder
- Direct Known Subclasses:
ServletUriComponentsBuilder
UriComponents
.
Typical usage involves:
- Create a
UriComponentsBuilder
with one of the static factory methods (such asfromPath(String)
orfromUri(URI)
) - Set the various URI components through the respective methods (
scheme(String)
,userInfo(String)
,host(String)
,port(int)
,path(String)
,pathSegment(String...)
,queryParam(String, Object...)
, andfragment(String)
. - Build the
UriComponents
instance with thebuild()
method.
- Since:
- 3.1
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Phillip Webb, Oliver Gierke, Brian Clozel, Sebastien Deleuze, Sam Brannen
- See Also:
-
Constructor Summary
ModifierConstructorDescriptionprotected
Default constructor.protected
Create a deep copy of the given UriComponentsBuilder. -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Build aUriComponents
instance from the various components contained in this builder.build
(boolean encoded) Variant ofbuild()
to create aUriComponents
instance when components are already fully encoded.Build aURI
instance and replaces URI template variables with the values from an array.Build aURI
instance and replaces URI template variables with the values from a map.buildAndExpand
(Object... uriVariableValues) Build aUriComponents
instance and replaces URI template variables with the values from an array.buildAndExpand
(Map<String, ?> uriVariables) Build aUriComponents
instance and replaces URI template variables with the values from a map.clone()
Public declaration of Object'sclone()
method.Clone thisUriComponentsBuilder
.final UriComponentsBuilder
encode()
Request to have the URI template pre-encoded at build time, and URI variables encoded separately when expanded.A variant ofencode()
with a charset other than "UTF-8".Set the URI fragment.static UriComponentsBuilder
fromHttpRequest
(HttpRequest request) Deprecated, for removal: This API element is subject to removal in a future version.static UriComponentsBuilder
fromHttpUrl
(String httpUrl) Deprecated.as of 6.2, in favor offromUriString(String)
; scheduled for removal in 8.0.static UriComponentsBuilder
fromOriginHeader
(String origin) Create an instance by parsing the "Origin" header of an HTTP request.static UriComponentsBuilder
Create a builder that is initialized with the given path.static UriComponentsBuilder
Create a builder that is initialized from the givenURI
.static UriComponentsBuilder
fromUriString
(String uri) Create a builder that is initialized with the given URI string.Set the URI host which may contain URI template variables, and may also benull
to clear the host of this builder.static UriComponentsBuilder
Create a new, empty builder.Append to the path of this builder.pathSegment
(String... pathSegments) Append to the path using path segments.port
(int port) Set the URI port.Set the URI port.Parse the given query string into query parameters where parameters are separated with'&'
and their values, if any, with'='
.queryParam
(String name, Object... values) Append the given query parameter.queryParam
(String name, Collection<?> values) Variant ofUriBuilder.queryParam(String, Object...)
with a Collection.queryParamIfPresent
(String name, Optional<?> value) Delegates to eitherUriBuilder.queryParam(String, Object...)
orUriBuilder.queryParam(String, Collection)
if the givenOptional
has a value, or else if it is empty, no query parameter is added at all.queryParams
(MultiValueMap<String, String> params) Add multiple query parameters and values.replacePath
(String path) Override the current path.replaceQuery
(String query) Clear existing query parameters and then delegate toUriBuilder.query(String)
.replaceQueryParam
(String name, Object... values) Set the query parameter values replacing existing values, or if no values are given, the query parameter is removed.replaceQueryParam
(String name, Collection<?> values) Variant ofUriBuilder.replaceQueryParam(String, Object...)
with a Collection.replaceQueryParams
(MultiValueMap<String, String> params) Set the query parameter values after removing all existing ones.Set the URI scheme which may contain URI template variables, and may also benull
to clear the scheme of this builder.schemeSpecificPart
(String ssp) Set the URI scheme-specific-part.Build a URI String.Initialize components of this builder from components of the given URI.uriComponents
(UriComponents uriComponents) Set or append individual URI components of this builder from the values of the givenUriComponents
instance.uriVariables
(Map<String, Object> uriVariables) Configure URI variables to be expanded at build time.Set the URI user info which may contain URI template variables, and may also benull
to clear the user info of this builder.
-
Constructor Details
-
UriComponentsBuilder
protected UriComponentsBuilder()Default constructor. Protected to prevent direct instantiation.- See Also:
-
UriComponentsBuilder
Create a deep copy of the given UriComponentsBuilder.- Parameters:
other
- the other builder to copy from- Since:
- 4.1.3
-
-
Method Details
-
newInstance
Create a new, empty builder.- Returns:
- the new
UriComponentsBuilder
-
fromPath
Create a builder that is initialized with the given path.- Parameters:
path
- the path to initialize with- Returns:
- the new
UriComponentsBuilder
-
fromUri
Create a builder that is initialized from the givenURI
.Note: the components in the resulting builder will be in fully encoded (raw) form and further changes must also supply values that are fully encoded, for example via methods in
UriUtils
. In addition please usebuild(boolean)
with a value of "true" to build theUriComponents
instance in order to indicate that the components are encoded.- Parameters:
uri
- the URI to initialize with- Returns:
- the new
UriComponentsBuilder
-
fromUriString
Create a builder that is initialized with the given URI string.Note: The presence of reserved characters can prevent correct parsing of the URI string. For example if a query parameter contains
'='
or'&'
characters, the query string cannot be parsed unambiguously. Such values should be substituted for URI variables to enable correct parsing:String uriString = "/hotels/42?filter={value}"; UriComponentsBuilder.fromUriString(uriString).buildAndExpand("hot&cold");
- Parameters:
uri
- the URI string to initialize with- Returns:
- the new
UriComponentsBuilder
- Throws:
InvalidUrlException
- ifuri
cannot be parsed
-
fromHttpUrl
@Deprecated(since="6.2") public static UriComponentsBuilder fromHttpUrl(String httpUrl) throws InvalidUrlException Deprecated.as of 6.2, in favor offromUriString(String)
; scheduled for removal in 8.0.Create a URI components builder from the given HTTP URL String.Note: The presence of reserved characters can prevent correct parsing of the URI string. For example if a query parameter contains
'='
or'&'
characters, the query string cannot be parsed unambiguously. Such values should be substituted for URI variables to enable correct parsing:String urlString = "https://example.com/hotels/42?filter={value}"; UriComponentsBuilder.fromHttpUrl(urlString).buildAndExpand("hot&cold");
- Parameters:
httpUrl
- the source URI- Returns:
- the URI components of the URI
- Throws:
InvalidUrlException
-
fromHttpRequest
@Deprecated(since="6.1", forRemoval=true) public static UriComponentsBuilder fromHttpRequest(HttpRequest request) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofForwardedHeaderUtils.adaptFromForwardedHeaders(java.net.URI, org.springframework.http.HttpHeaders)
; to be removed in 6.2Create a newUriComponents
object from the URI associated with the given HttpRequest while also overlaying with values from the headers "Forwarded" (RFC 7239), or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if "Forwarded" is not found.- Parameters:
request
- the source request- Returns:
- the URI components of the URI
- Since:
- 4.1.5
-
fromOriginHeader
Create an instance by parsing the "Origin" header of an HTTP request.- See Also:
-
encode
Request to have the URI template pre-encoded at build time, and URI variables encoded separately when expanded.In comparison to
UriComponents.encode()
, this method has the same effect on the URI template, i.e. each URI component is encoded by replacing non-ASCII and illegal (within the URI component type) characters with escaped octets. However URI variables are encoded more strictly, by also escaping characters with reserved meaning.For most cases, this method is more likely to give the expected result because in treats URI variables as opaque data to be fully encoded, while
UriComponents.encode()
is useful when intentionally expanding URI variables that contain reserved characters.For example ';' is legal in a path but has reserved meaning. This method replaces ";" with "%3B" in URI variables but not in the URI template. By contrast,
UriComponents.encode()
never replaces ";" since it is a legal character in a path.When not expanding URI variables at all, prefer use of
UriComponents.encode()
since that will also encode anything that incidentally looks like a URI variable.- Since:
- 5.0.8
-
encode
A variant ofencode()
with a charset other than "UTF-8".- Parameters:
charset
- the charset to use for encoding- Since:
- 5.0.8
-
build
Build aUriComponents
instance from the various components contained in this builder.- Returns:
- the URI components
-
build
Variant ofbuild()
to create aUriComponents
instance when components are already fully encoded. This is useful for example if the builder was created viafromUri(URI)
.- Parameters:
encoded
- whether the components in this builder are already encoded- Returns:
- the URI components
- Throws:
IllegalArgumentException
- if any of the components contain illegal characters that should have been encoded.
-
buildAndExpand
Build aUriComponents
instance and replaces URI template variables with the values from a map. This is a shortcut method which combines calls tobuild()
and thenUriComponents.expand(Map)
.- Parameters:
uriVariables
- the map of URI variables- Returns:
- the URI components with expanded values
-
buildAndExpand
Build aUriComponents
instance and replaces URI template variables with the values from an array. This is a shortcut method which combines calls tobuild()
and thenUriComponents.expand(Object...)
.- Parameters:
uriVariableValues
- the URI variable values- Returns:
- the URI components with expanded values
-
build
Description copied from interface:UriBuilder
Build aURI
instance and replaces URI template variables with the values from an array.- Specified by:
build
in interfaceUriBuilder
- Parameters:
uriVariables
- the map of URI variables- Returns:
- the URI
-
build
Description copied from interface:UriBuilder
Build aURI
instance and replaces URI template variables with the values from a map.- Specified by:
build
in interfaceUriBuilder
- Parameters:
uriVariables
- the map of URI variables- Returns:
- the URI
-
toUriString
Build a URI String.Effectively, a shortcut for building, encoding, and returning the String representation:
String uri = builder.build().encode().toUriString()
However if
URI variables
have been provided then the URI template is pre-encoded separately from URI variables (seeencode()
for details), i.e. equivalent to:String uri = builder.encode().build().toUriString()
- Specified by:
toUriString
in interfaceUriBuilder
- Since:
- 4.1
- See Also:
-
uri
Initialize components of this builder from components of the given URI.- Parameters:
uri
- the URI- Returns:
- this UriComponentsBuilder
-
uriComponents
Set or append individual URI components of this builder from the values of the givenUriComponents
instance.For the semantics of each component (i.e. set vs append) check the builder methods on this class. For example
host(String)
sets whilepath(String)
appends.- Parameters:
uriComponents
- the UriComponents to copy from- Returns:
- this UriComponentsBuilder
-
scheme
Description copied from interface:UriBuilder
Set the URI scheme which may contain URI template variables, and may also benull
to clear the scheme of this builder.- Specified by:
scheme
in interfaceUriBuilder
- Parameters:
scheme
- the URI scheme
-
schemeSpecificPart
Set the URI scheme-specific-part. When invoked, this method overwrites user-info, host, port, path, andquery
.- Parameters:
ssp
- the URI scheme-specific-part, may contain URI template parameters- Returns:
- this UriComponentsBuilder
-
userInfo
Description copied from interface:UriBuilder
Set the URI user info which may contain URI template variables, and may also benull
to clear the user info of this builder.- Specified by:
userInfo
in interfaceUriBuilder
- Parameters:
userInfo
- the URI user info
-
host
Description copied from interface:UriBuilder
Set the URI host which may contain URI template variables, and may also benull
to clear the host of this builder.- Specified by:
host
in interfaceUriBuilder
- Parameters:
host
- the URI host
-
port
Description copied from interface:UriBuilder
Set the URI port. Passing-1
will clear the port of this builder.- Specified by:
port
in interfaceUriBuilder
- Parameters:
port
- the URI port
-
port
Description copied from interface:UriBuilder
Set the URI port. Use this method only when the port needs to be parameterized with a URI variable. Otherwise useUriBuilder.port(int)
. Passingnull
will clear the port of this builder.- Specified by:
port
in interfaceUriBuilder
- Parameters:
port
- the URI port
-
path
Description copied from interface:UriBuilder
Append to the path of this builder.The given value is appended as-is to previous
path
values without inserting any additional slashes. For example:builder.path("/first-").path("value/").path("/{id}").build("123") // Results is "/first-value/123"
By contrast
pathSegment
does insert slashes between individual path segments. For example:builder.pathSegment("first-value", "second-value").path("/") // Results is "/first-value/second-value/"
The resulting full path is normalized to eliminate duplicate slashes.
Note: When inserting a URI variable value that contains slashes in a
path
, whether those are encoded depends on the configured encoding mode. For more details, seeencode()
, or otherwise if building URIs indirectly viaWebClient
orRestTemplate
, see itsencodingMode
. Also see the URI Encoding section of the reference docs.- Specified by:
path
in interfaceUriBuilder
- Parameters:
path
- the URI path
-
pathSegment
Description copied from interface:UriBuilder
Append to the path using path segments. For example:builder.pathSegment("first-value", "second-value", "{id}").build("123") // Results is "/first-value/second-value/123"
If slashes are present in a path segment, they are encoded:
builder.pathSegment("ba/z", "{id}").build("a/b") // Results is "/ba%2Fz/a%2Fb"
To insert a trailing slash, use theUriBuilder.path(java.lang.String)
builder method:builder.pathSegment("first-value", "second-value").path("/") // Results is "/first-value/second-value/"
Empty path segments are ignored and therefore duplicate slashes do not appear in the resulting full path.
- Specified by:
pathSegment
in interfaceUriBuilder
- Parameters:
pathSegments
- the URI path segments- Throws:
IllegalArgumentException
-
replacePath
Description copied from interface:UriBuilder
Override the current path.- Specified by:
replacePath
in interfaceUriBuilder
- Parameters:
path
- the URI path, ornull
for an empty path
-
query
Description copied from interface:UriBuilder
Parse the given query string into query parameters where parameters are separated with'&'
and their values, if any, with'='
. The query may contain URI template variables.Note: please, review the Javadoc of
UriBuilder.queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- Specified by:
query
in interfaceUriBuilder
- Parameters:
query
- the query string
-
replaceQuery
Description copied from interface:UriBuilder
Clear existing query parameters and then delegate toUriBuilder.query(String)
.Note: please, review the Javadoc of
UriBuilder.queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- Specified by:
replaceQuery
in interfaceUriBuilder
- Parameters:
query
- the query string; anull
value removes all query parameters.
-
queryParam
Description copied from interface:UriBuilder
Append the given query parameter. Both the parameter name and values may contain URI template variables to be expanded later from values. If no values are given, the resulting URI will contain the query parameter name only, e.g."?foo"
instead of"?foo=bar"
.Note: encoding, if applied, will only encode characters that are illegal in a query parameter name or value such as
"="
or"&"
. All others that are legal as per syntax rules in RFC 3986 are not encoded. This includes"+"
which sometimes needs to be encoded to avoid its interpretation as an encoded space. Stricter encoding may be applied by using a URI template variable along with stricter encoding on variable values. For more details please read the "URI Encoding" section of the Spring Framework reference.- Specified by:
queryParam
in interfaceUriBuilder
- Parameters:
name
- the query parameter namevalues
- the query parameter values- See Also:
-
queryParam
Description copied from interface:UriBuilder
Variant ofUriBuilder.queryParam(String, Object...)
with a Collection.Note: please, review the Javadoc of
UriBuilder.queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- Specified by:
queryParam
in interfaceUriBuilder
- Parameters:
name
- the query parameter namevalues
- the query parameter values- See Also:
-
queryParamIfPresent
Description copied from interface:UriBuilder
Delegates to eitherUriBuilder.queryParam(String, Object...)
orUriBuilder.queryParam(String, Collection)
if the givenOptional
has a value, or else if it is empty, no query parameter is added at all.- Specified by:
queryParamIfPresent
in interfaceUriBuilder
- Parameters:
name
- the query parameter namevalue
- an Optional, either empty or holding the query parameter value.
-
queryParams
Add multiple query parameters and values.Note: please, review the Javadoc of
UriBuilder.queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- Specified by:
queryParams
in interfaceUriBuilder
- Parameters:
params
- the params- Since:
- 4.0
-
replaceQueryParam
Description copied from interface:UriBuilder
Set the query parameter values replacing existing values, or if no values are given, the query parameter is removed.Note: please, review the Javadoc of
UriBuilder.queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- Specified by:
replaceQueryParam
in interfaceUriBuilder
- Parameters:
name
- the query parameter namevalues
- the query parameter values- See Also:
-
replaceQueryParam
Description copied from interface:UriBuilder
Variant ofUriBuilder.replaceQueryParam(String, Object...)
with a Collection.Note: please, review the Javadoc of
UriBuilder.queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- Specified by:
replaceQueryParam
in interfaceUriBuilder
- Parameters:
name
- the query parameter namevalues
- the query parameter values- See Also:
-
replaceQueryParams
Set the query parameter values after removing all existing ones.Note: please, review the Javadoc of
UriBuilder.queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- Specified by:
replaceQueryParams
in interfaceUriBuilder
- Parameters:
params
- the query parameter name- Since:
- 4.2
-
fragment
Description copied from interface:UriBuilder
Set the URI fragment. The given fragment may contain URI template variables, and may also benull
to clear the fragment of this builder.- Specified by:
fragment
in interfaceUriBuilder
- Parameters:
fragment
- the URI fragment
-
uriVariables
Configure URI variables to be expanded at build time.The provided variables may be a subset of all required ones. At build time, the available ones are expanded, while unresolved URI placeholders are left in place and can still be expanded later.
In contrast to
UriComponents.expand(Map)
orbuildAndExpand(Map)
, this method is useful when you need to supply URI variables without building theUriComponents
instance just yet, or perhaps pre-expand some shared default values such as host and port.- Parameters:
uriVariables
- the URI variables to use- Returns:
- this UriComponentsBuilder
- Since:
- 5.0.8
-
clone
Public declaration of Object'sclone()
method. Delegates tocloneBuilder()
. -
cloneBuilder
Clone thisUriComponentsBuilder
.- Returns:
- the cloned
UriComponentsBuilder
object - Since:
- 4.2.7
-
ForwardedHeaderUtils.adaptFromForwardedHeaders(java.net.URI, org.springframework.http.HttpHeaders)
; to be removed in 6.2