public class UriComponentsBuilder
extends java.lang.Object
UriComponents
.
Typical usage involves:
UriComponentsBuilder
with one of the static factory methods (such as
fromPath(String)
or fromUri(URI)
)scheme(String)
,
userInfo(String)
, host(String)
, port(int)
, path(String)
,
pathSegment(String...)
, queryParam(String, Object...)
, and
fragment(String)
.UriComponents
instance with the build()
method.newInstance()
,
fromPath(String)
,
fromUri(URI)
Modifier and Type | Class and Description |
---|---|
private static class |
UriComponentsBuilder.CompositePathComponentBuilder |
private static class |
UriComponentsBuilder.FullPathComponentBuilder |
private static interface |
UriComponentsBuilder.PathComponentBuilder |
private static class |
UriComponentsBuilder.PathSegmentComponentBuilder |
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
fragment |
private java.lang.String |
host |
private static java.lang.String |
HOST_PATTERN |
private static java.lang.String |
HTTP_PATTERN |
private static java.util.regex.Pattern |
HTTP_URL_PATTERN |
private static java.lang.String |
LAST_PATTERN |
private static java.lang.String |
PATH_PATTERN |
private UriComponentsBuilder.CompositePathComponentBuilder |
pathBuilder |
private int |
port |
private static java.lang.String |
PORT_PATTERN |
private static java.util.regex.Pattern |
QUERY_PARAM_PATTERN |
private static java.lang.String |
QUERY_PATTERN |
private MultiValueMap<java.lang.String,java.lang.String> |
queryParams |
private java.lang.String |
scheme |
private static java.lang.String |
SCHEME_PATTERN |
private java.lang.String |
ssp |
private static java.util.regex.Pattern |
URI_PATTERN |
private java.lang.String |
userInfo |
private static java.lang.String |
USERINFO_PATTERN |
Modifier | Constructor and Description |
---|---|
protected |
UriComponentsBuilder()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
UriComponents |
build()
Builds a
UriComponents instance from the various components contained in this builder. |
UriComponents |
build(boolean encoded)
Builds a
UriComponents instance from the various components
contained in this builder. |
UriComponents |
buildAndExpand(java.util.Map<java.lang.String,?> uriVariables)
Builds a
UriComponents instance and replaces URI template variables
with the values from a map. |
UriComponents |
buildAndExpand(java.lang.Object... uriVariableValues)
Builds a
UriComponents instance and replaces URI template variables
with the values from an array. |
UriComponentsBuilder |
fragment(java.lang.String fragment)
Sets the URI fragment.
|
static UriComponentsBuilder |
fromHttpUrl(java.lang.String httpUrl)
Creates a new
UriComponents object from the string HTTP URL. |
static UriComponentsBuilder |
fromPath(java.lang.String path)
Returns a builder that is initialized with the given path.
|
static UriComponentsBuilder |
fromUri(java.net.URI uri)
Returns a builder that is initialized with the given
URI . |
static UriComponentsBuilder |
fromUriString(java.lang.String uri)
Returns a builder that is initialized with the given URI string.
|
UriComponentsBuilder |
host(java.lang.String host)
Sets the URI host.
|
static UriComponentsBuilder |
newInstance()
Returns a new, empty builder.
|
UriComponentsBuilder |
path(java.lang.String path)
Appends the given path to the existing path of this builder.
|
UriComponentsBuilder |
pathSegment(java.lang.String... pathSegments)
Appends the given path segments to the existing path of this builder.
|
UriComponentsBuilder |
port(int port)
Sets the URI port.
|
UriComponentsBuilder |
query(java.lang.String query)
Appends the given query to the existing query of this builder.
|
UriComponentsBuilder |
queryParam(java.lang.String name,
java.lang.Object... values)
Appends the given query parameter to the existing query parameters.
|
UriComponentsBuilder |
replacePath(java.lang.String path)
Sets the path of this builder overriding all existing path and path segment values.
|
UriComponentsBuilder |
replaceQuery(java.lang.String query)
Sets the query of this builder overriding all existing query parameters.
|
UriComponentsBuilder |
replaceQueryParam(java.lang.String name,
java.lang.Object... values)
Sets the query parameter values overriding all existing query values for
the same parameter.
|
private void |
resetHierarchicalComponents() |
private void |
resetSchemeSpecificPart() |
UriComponentsBuilder |
scheme(java.lang.String scheme)
Sets the URI scheme.
|
UriComponentsBuilder |
schemeSpecificPart(java.lang.String ssp)
Set the URI scheme-specific-part.
|
UriComponentsBuilder |
uri(java.net.URI uri)
Initializes all components of this URI builder with the components of the given URI.
|
UriComponentsBuilder |
userInfo(java.lang.String userInfo)
Sets the URI user info.
|
private static final java.util.regex.Pattern QUERY_PARAM_PATTERN
private static final java.lang.String SCHEME_PATTERN
private static final java.lang.String HTTP_PATTERN
private static final java.lang.String USERINFO_PATTERN
private static final java.lang.String HOST_PATTERN
private static final java.lang.String PORT_PATTERN
private static final java.lang.String PATH_PATTERN
private static final java.lang.String QUERY_PATTERN
private static final java.lang.String LAST_PATTERN
private static final java.util.regex.Pattern URI_PATTERN
private static final java.util.regex.Pattern HTTP_URL_PATTERN
private java.lang.String scheme
private java.lang.String ssp
private java.lang.String userInfo
private java.lang.String host
private int port
private UriComponentsBuilder.CompositePathComponentBuilder pathBuilder
private final MultiValueMap<java.lang.String,java.lang.String> queryParams
private java.lang.String fragment
protected UriComponentsBuilder()
newInstance()
,
fromPath(String)
,
fromUri(URI)
public static UriComponentsBuilder newInstance()
UriComponentsBuilder
public static UriComponentsBuilder fromPath(java.lang.String path)
path
- the path to initialize withUriComponentsBuilder
public static UriComponentsBuilder fromUri(java.net.URI uri)
URI
.uri
- the URI to initialize withUriComponentsBuilder
public static UriComponentsBuilder fromUriString(java.lang.String uri)
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");
uri
- the URI string to initialize withUriComponentsBuilder
public static UriComponentsBuilder fromHttpUrl(java.lang.String httpUrl)
UriComponents
object from the string HTTP URL.
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");
httpUrl
- the source URIpublic UriComponents build()
UriComponents
instance from the various components contained in this builder.public UriComponents build(boolean encoded)
UriComponents
instance from the various components
contained in this builder.encoded
- whether all the components set in this builder are
encoded (true
) or not (false
).public UriComponents buildAndExpand(java.util.Map<java.lang.String,?> uriVariables)
UriComponents
instance and replaces URI template variables
with the values from a map. This is a shortcut method, which combines
calls to build()
and then UriComponents.expand(Map)
.uriVariables
- the map of URI variablespublic UriComponents buildAndExpand(java.lang.Object... uriVariableValues)
UriComponents
instance and replaces URI template variables
with the values from an array. This is a shortcut method, which combines
calls to build()
and then UriComponents.expand(Object...)
.uriVariableValues
- URI variable valuespublic UriComponentsBuilder uri(java.net.URI uri)
uri
- the URIprivate void resetHierarchicalComponents()
private void resetSchemeSpecificPart()
public UriComponentsBuilder scheme(java.lang.String scheme)
null
to clear the scheme of this builder.scheme
- the URI schemepublic UriComponentsBuilder schemeSpecificPart(java.lang.String ssp)
query
.ssp
- the URI scheme-specific-part, may contain URI template parameterspublic UriComponentsBuilder userInfo(java.lang.String userInfo)
null
to clear the user info of this
builder.userInfo
- the URI user infopublic UriComponentsBuilder host(java.lang.String host)
null
to clear the host of this builder.host
- the URI hostpublic UriComponentsBuilder port(int port)
-1
will clear the port of this builder.port
- the URI portpublic UriComponentsBuilder path(java.lang.String path)
path
- the URI pathpublic UriComponentsBuilder replacePath(java.lang.String path)
path
- the URI path; a null
value results in an empty path.public UriComponentsBuilder pathSegment(java.lang.String... pathSegments) throws java.lang.IllegalArgumentException
pathSegments
- the URI path segmentsjava.lang.IllegalArgumentException
public UriComponentsBuilder query(java.lang.String query)
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");
query
- the query stringpublic UriComponentsBuilder replaceQuery(java.lang.String query)
query
- the query string; a null
value removes all query parameters.public UriComponentsBuilder queryParam(java.lang.String name, java.lang.Object... values)
?foo
instead of ?foo=bar
.name
- the query parameter namevalues
- the query parameter valuespublic UriComponentsBuilder replaceQueryParam(java.lang.String name, java.lang.Object... values)
name
- the query parameter namevalues
- the query parameter valuespublic UriComponentsBuilder fragment(java.lang.String fragment)
null
to clear the fragment of this
builder.fragment
- the URI fragment