Package org.springframework.web.util
Class DefaultUriBuilderFactory
- java.lang.Object
- org.springframework.web.util.DefaultUriBuilderFactory
- All Implemented Interfaces:
UriBuilderFactory
,UriTemplateHandler
public class DefaultUriBuilderFactory extends Object implements UriBuilderFactory
UriBuilderFactory
that relies onUriComponentsBuilder
for the actual building of the URI.Provides options to create
UriBuilder
instances with a common base URI, alternative encoding mode strategies, among others.- Since:
- 5.0
- Author:
- Rossen Stoyanchev
- See Also:
UriComponentsBuilder
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DefaultUriBuilderFactory.EncodingMode
Enum to represent multiple URI encoding strategies.
Constructor Summary
Constructors Constructor Description DefaultUriBuilderFactory()
Default constructor without a base URI.DefaultUriBuilderFactory(String baseUriTemplate)
Constructor with a base URI.DefaultUriBuilderFactory(UriComponentsBuilder baseUri)
Variant ofDefaultUriBuilderFactory(String)
with aUriComponentsBuilder
.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description UriBuilder
builder()
Create a URI builder with default settings.URI
expand(String uriTemplate, Object... uriVars)
Expand the given URI template with an array of URI variables.URI
expand(String uriTemplate, Map<String,?> uriVars)
Expand the given URI template with a map of URI variables.Map<String,?>
getDefaultUriVariables()
Return the configured default URI variable values.DefaultUriBuilderFactory.EncodingMode
getEncodingMode()
Return the configured encoding mode.void
setDefaultUriVariables(Map<String,?> defaultUriVariables)
Provide default URI variable values to use when expanding URI templates with a Map of variables.void
setEncodingMode(DefaultUriBuilderFactory.EncodingMode encodingMode)
Set theencoding mode
to use.void
setParsePath(boolean parsePath)
Whether to parse the input path into path segments if the encoding mode is set toEncodingMode.URI_COMPONENT
, which ensures that URI variables in the path are encoded according to path segment rules and for example a '/' is encoded.boolean
shouldParsePath()
Whether to parse the path into path segments if the encoding mode is set toEncodingMode.URI_COMPONENT
.UriBuilder
uriString(String uriTemplate)
Initialize a builder with the given URI template.
Constructor Detail
DefaultUriBuilderFactory
public DefaultUriBuilderFactory()
Default constructor without a base URI.The target address must be specified on each UriBuilder.
DefaultUriBuilderFactory
public DefaultUriBuilderFactory(String baseUriTemplate)
Constructor with a base URI.The given URI template is parsed via
UriComponentsBuilder.fromUriString(java.lang.String)
and then applied as a base URI to every UriBuilder viaUriComponentsBuilder.uriComponents(org.springframework.web.util.UriComponents)
unless the UriBuilder itself was created with a URI template that already has a target address.- Parameters:
baseUriTemplate
- the URI template to use a base URL
DefaultUriBuilderFactory
public DefaultUriBuilderFactory(UriComponentsBuilder baseUri)
Variant ofDefaultUriBuilderFactory(String)
with aUriComponentsBuilder
.
Method Detail
setEncodingMode
public void setEncodingMode(DefaultUriBuilderFactory.EncodingMode encodingMode)
Set theencoding mode
to use.By default this is set to
EncodingMode.TEMPLATE_AND_VALUES
.Note: Prior to 5.1 the default was
EncodingMode.URI_COMPONENT
therefore theWebClient
RestTemplate
have switched their default behavior.- Parameters:
encodingMode
- the encoding mode to use
getEncodingMode
public DefaultUriBuilderFactory.EncodingMode getEncodingMode()
Return the configured encoding mode.
setDefaultUriVariables
public void setDefaultUriVariables(@Nullable Map<String,?> defaultUriVariables)
Provide default URI variable values to use when expanding URI templates with a Map of variables.- Parameters:
defaultUriVariables
- default URI variable values
getDefaultUriVariables
public Map<String,?> getDefaultUriVariables()
Return the configured default URI variable values.
setParsePath
public void setParsePath(boolean parsePath)
Whether to parse the input path into path segments if the encoding mode is set toEncodingMode.URI_COMPONENT
, which ensures that URI variables in the path are encoded according to path segment rules and for example a '/' is encoded.By default this is set to
true
.- Parameters:
parsePath
- whether to parse the path into path segments
shouldParsePath
public boolean shouldParsePath()
Whether to parse the path into path segments if the encoding mode is set toEncodingMode.URI_COMPONENT
.
expand
public URI expand(String uriTemplate, Map<String,?> uriVars)
Description copied from interface:UriTemplateHandler
Expand the given URI template with a map of URI variables.- Specified by:
expand
in interfaceUriTemplateHandler
- Parameters:
uriTemplate
- the URI templateuriVars
- variable values- Returns:
- the created URI instance
expand
public URI expand(String uriTemplate, Object... uriVars)
Description copied from interface:UriTemplateHandler
Expand the given URI template with an array of URI variables.- Specified by:
expand
in interfaceUriTemplateHandler
- Parameters:
uriTemplate
- the URI templateuriVars
- variable values- Returns:
- the created URI instance
uriString
public UriBuilder uriString(String uriTemplate)
Description copied from interface:UriBuilderFactory
Initialize a builder with the given URI template.- Specified by:
uriString
in interfaceUriBuilderFactory
- Parameters:
uriTemplate
- the URI template to use- Returns:
- the builder instance
builder
public UriBuilder builder()
Description copied from interface:UriBuilderFactory
Create a URI builder with default settings.- Specified by:
builder
in interfaceUriBuilderFactory
- Returns:
- the builder instance