接口 UriBuilder
public interface UriBuilder
Builder-style methods to prepare and expand a URI template with variables.Effectively a generalization of
UriComponentsBuilder
but with shortcuts to expand directly intoURI
rather thanUriComponents
and also leaving common concerns such as encoding preferences, a base URI, and others as implementation concerns.Typically obtained via
UriBuilderFactory
which serves as a central component configured once and used to create many URLs.- 从以下版本开始:
- 5.0
- 作者:
- Rossen Stoyanchev
- 另请参阅:
UriBuilderFactory
,UriComponentsBuilder
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 URI
build(Object... uriVariables)
Build aURI
instance and replaces URI template variables with the values from an array.URI
build(Map<String,?> uriVariables)
Build aURI
instance and replaces URI template variables with the values from a map.UriBuilder
fragment(String fragment)
Set the URI fragment.UriBuilder
host(String host)
Set the URI host which may contain URI template variables, and may also benull
to clear the host of this builder.UriBuilder
path(String path)
Append to the path of this builder.UriBuilder
pathSegment(String... pathSegments)
Append to the path using path segments.UriBuilder
port(int port)
Set the URI port.UriBuilder
port(String port)
Set the URI port .UriBuilder
query(String query)
Parse the given query string into query parameters where parameters are separated with'&'
and their values, if any, with'='
.UriBuilder
queryParam(String name, Object... values)
Append the given query parameter.UriBuilder
queryParam(String name, Collection<?> values)
Variant ofqueryParam(String, Object...)
with a Collection.UriBuilder
queryParams(MultiValueMap<String,String> params)
Add multiple query parameters and values.UriBuilder
replacePath(String path)
Override the existing path.UriBuilder
replaceQuery(String query)
Clear existing query parameters and then delegate toquery(String)
.UriBuilder
replaceQueryParam(String name, Object... values)
Set the query parameter values replacing existing values, or if no values are given, the query parameter is removed.UriBuilder
replaceQueryParam(String name, Collection<?> values)
Variant ofreplaceQueryParam(String, Object...)
with a Collection.UriBuilder
replaceQueryParams(MultiValueMap<String,String> params)
Set the query parameter values after removing all existing ones.UriBuilder
scheme(String scheme)
Set the URI scheme which may contain URI template variables, and may also benull
to clear the scheme of this builder.UriBuilder
userInfo(String userInfo)
Set the URI user info which may contain URI template variables, and may also benull
to clear the user info of this builder.
方法详细资料
scheme
UriBuilder scheme(@Nullable String scheme)
Set the URI scheme which may contain URI template variables, and may also benull
to clear the scheme of this builder.- 参数:
scheme
- the URI scheme
userInfo
UriBuilder userInfo(@Nullable String userInfo)
Set the URI user info which may contain URI template variables, and may also benull
to clear the user info of this builder.- 参数:
userInfo
- the URI user info
host
UriBuilder host(@Nullable String host)
Set the URI host which may contain URI template variables, and may also benull
to clear the host of this builder.- 参数:
host
- the URI host
port
UriBuilder port(int port)
Set the URI port. Passing-1
will clear the port of this builder.- 参数:
port
- the URI port
port
UriBuilder port(@Nullable String port)
Set the URI port . Use this method only when the port needs to be parameterized with a URI variable. Otherwise useport(int)
. Passingnull
will clear the port of this builder.- 参数:
port
- the URI port
path
UriBuilder path(String path)
Append to the path of this builder.The given value is appended as-is without any checks for slashes other than to clean up duplicates. For example:
builder.path("/first-").path("value/").path("/{id}").build("123") // Results is "/first-value/123"
By contrast
pathSegment(String...)
builds the path from individual path segments and in that case slashes are inserted transparently. In some cases you may use a combination of bothpathSegment
andpath
. For example:builder.pathSegment("first-value", "second-value").path("/") // Results is "/first-value/second-value/"
If a URI variable value contains slashes, whether those are encoded or not depends on the configured encoding mode. See
UriComponentsBuilder.encode()
, or if usingUriComponentsBuilder
viaDefaultUriBuilderFactory
(e.g.WebClient
orRestTemplate
) see itsencodingMode
property. Also see the URI Encoding section of the reference docs.- 参数:
path
- the URI path
replacePath
UriBuilder replacePath(@Nullable String path)
Override the existing path.- 参数:
path
- the URI path, ornull
for an empty path
pathSegment
UriBuilder pathSegment(String... pathSegments) throws IllegalArgumentException
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 thepath(java.lang.String)
builder method:builder.pathSegment("first-value", "second-value").path("/") // Results is "/first-value/second-value/"
- 参数:
pathSegments
- the URI path segments- 抛出:
IllegalArgumentException
query
UriBuilder query(String query)
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
queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- 参数:
query
- the query string
replaceQuery
UriBuilder replaceQuery(@Nullable String query)
Clear existing query parameters and then delegate toquery(String)
.Note: please, review the Javadoc of
queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- 参数:
query
- the query string; anull
value removes all query parameters.
queryParam
UriBuilder queryParam(String name, Object... values)
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.- 参数:
name
- the query parameter namevalues
- the query parameter values- 另请参阅:
queryParam(String, Collection)
queryParam
UriBuilder queryParam(String name, @Nullable Collection<?> values)
Variant ofqueryParam(String, Object...)
with a Collection.Note: please, review the Javadoc of
queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- 参数:
name
- the query parameter namevalues
- the query parameter values- 从以下版本开始:
- 5.2
- 另请参阅:
queryParam(String, Object...)
queryParams
UriBuilder queryParams(MultiValueMap<String,String> params)
Add multiple query parameters and values.Note: please, review the Javadoc of
queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- 参数:
params
- the params
replaceQueryParam
UriBuilder replaceQueryParam(String name, Object... values)
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
queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- 参数:
name
- the query parameter namevalues
- the query parameter values- 另请参阅:
replaceQueryParam(String, Collection)
replaceQueryParam
UriBuilder replaceQueryParam(String name, @Nullable Collection<?> values)
Variant ofreplaceQueryParam(String, Object...)
with a Collection.Note: please, review the Javadoc of
queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- 参数:
name
- the query parameter namevalues
- the query parameter values- 从以下版本开始:
- 5.2
- 另请参阅:
replaceQueryParam(String, Object...)
replaceQueryParams
UriBuilder replaceQueryParams(MultiValueMap<String,String> params)
Set the query parameter values after removing all existing ones.Note: please, review the Javadoc of
queryParam(String, Object...)
for further notes on the treatment and encoding of individual query parameters.- 参数:
params
- the query parameter name
fragment
UriBuilder fragment(@Nullable String fragment)
Set the URI fragment. The given fragment may contain URI template variables, and may also benull
to clear the fragment of this builder.- 参数:
fragment
- the URI fragment
build
URI build(Object... uriVariables)
Build aURI
instance and replaces URI template variables with the values from an array.- 参数:
uriVariables
- the map of URI variables- 返回:
- the URI