类 UriComponentsBuilder
- java.lang.Object
- org.springframework.web.util.UriComponentsBuilder
- 所有已实现的接口:
Cloneable
- 直接已知子类:
ServletUriComponentsBuilder
public class UriComponentsBuilder extends Object implements Cloneable
Builder forUriComponents.Typical usage involves:
- Create a
UriComponentsBuilderwith 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
UriComponentsinstance with thebuild()method.
- 从以下版本开始:
- 3.1
- 作者:
- Arjen Poutsma, Rossen Stoyanchev, Phillip Webb, Oliver Gierke, Brian Clozel, Sebastien Deleuze
- 另请参阅:
newInstance(),fromPath(String),fromUri(URI)
构造器概要
构造器 限定符 构造器 说明 protectedUriComponentsBuilder()Default constructor.protectedUriComponentsBuilder(UriComponentsBuilder other)Create a deep copy of the given UriComponentsBuilder.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 UriComponentsbuild()Build aUriComponentsinstance from the various components contained in this builder.UriComponentsbuild(boolean encoded)Variant ofbuild()to create aUriComponentsinstance when components are already fully encoded.UriComponentsbuildAndExpand(Object... uriVariableValues)Build aUriComponentsinstance and replaces URI template variables with the values from an array.UriComponentsbuildAndExpand(Map<String,?> uriVariables)Build aUriComponentsinstance and replaces URI template variables with the values from a map.Objectclone()Public declaration of Object'sclone()method.UriComponentsBuildercloneBuilder()Clone thisUriComponentsBuilder.UriComponentsBuilderfragment(String fragment)Set the URI fragment.static UriComponentsBuilderfromHttpRequest(HttpRequest request)Create a newUriComponentsobject 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.static UriComponentsBuilderfromHttpUrl(String httpUrl)Create a URI components builder from the given HTTP URL String.static UriComponentsBuilderfromOriginHeader(String origin)Create an instance by parsing the "Origin" header of an HTTP request.static UriComponentsBuilderfromPath(String path)Create a builder that is initialized with the given path.static UriComponentsBuilderfromUri(URI uri)Create a builder that is initialized from the givenURI.static UriComponentsBuilderfromUriString(String uri)Create a builder that is initialized with the given URI string.UriComponentsBuilderhost(String host)Set the URI host.static UriComponentsBuildernewInstance()Create a new, empty builder.UriComponentsBuilderpath(String path)Append the given path to the existing path of this builder.UriComponentsBuilderpathSegment(String... pathSegments)Append path segments to the existing path.UriComponentsBuilderport(int port)Set the URI port.UriComponentsBuilderport(String port)Set the URI port.UriComponentsBuilderquery(String query)Append the given query to the existing query of this builder.UriComponentsBuilderqueryParam(String name, Object... values)Append the given query parameter to the existing query parameters.UriComponentsBuilderqueryParams(MultiValueMap<String,String> params)Add the given query parameters.UriComponentsBuilderreplacePath(String path)Set the path of this builder overriding all existing path and path segment values.UriComponentsBuilderreplaceQuery(String query)Set the query of this builder overriding all existing query parameters.UriComponentsBuilderreplaceQueryParam(String name, Object... values)Set the query parameter values overriding all existing query values for the same parameter.UriComponentsBuilderreplaceQueryParams(MultiValueMap<String,String> params)Set the query parameter values overriding all existing query values.UriComponentsBuilderscheme(String scheme)Set the URI scheme.UriComponentsBuilderschemeSpecificPart(String ssp)Set the URI scheme-specific-part.StringtoUriString()Build a URI String.UriComponentsBuilderuri(URI uri)Initialize components of this builder from components of the given URI.UriComponentsBuilderuriComponents(UriComponents uriComponents)Set or append individual URI components of this builder from the values of the givenUriComponentsinstance.UriComponentsBuilderuserInfo(String userInfo)Set the URI user info.
构造器详细资料
UriComponentsBuilder
protected UriComponentsBuilder()
Default constructor. Protected to prevent direct instantiation.
UriComponentsBuilder
protected UriComponentsBuilder(UriComponentsBuilder other)
Create a deep copy of the given UriComponentsBuilder.- 参数:
other- the other builder to copy from- 从以下版本开始:
- 4.1.3
方法详细资料
newInstance
public static UriComponentsBuilder newInstance()
Create a new, empty builder.- 返回:
- the new
UriComponentsBuilder
fromPath
public static UriComponentsBuilder fromPath(String path)
Create a builder that is initialized with the given path.- 参数:
path- the path to initialize with- 返回:
- the new
UriComponentsBuilder
fromUri
public static UriComponentsBuilder fromUri(URI uri)
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 theUriComponentsinstance in order to indicate that the components are encoded.- 参数:
uri- the URI to initialize with- 返回:
- the new
UriComponentsBuilder
fromUriString
public static UriComponentsBuilder fromUriString(String uri)
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");- 参数:
uri- the URI string to initialize with- 返回:
- the new
UriComponentsBuilder
fromHttpUrl
public static UriComponentsBuilder fromHttpUrl(String httpUrl)
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");- 参数:
httpUrl- the source URI- 返回:
- the URI components of the URI
fromHttpRequest
public static UriComponentsBuilder fromHttpRequest(HttpRequest request)
Create a newUriComponentsobject 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.Note: this method uses values from forwarded headers, if present, in order to reflect the client-originated protocol and address. Consider using the
ForwardedHeaderFilterin order to choose from a central place whether to extract and use, or to discard such headers. See the Spring Framework reference for more on this filter.- 参数:
request- the source request- 返回:
- the URI components of the URI
- 从以下版本开始:
- 4.1.5
fromOriginHeader
public static UriComponentsBuilder fromOriginHeader(String origin)
Create an instance by parsing the "Origin" header of an HTTP request.- 另请参阅:
- RFC 6454
build
public UriComponents build()
Build aUriComponentsinstance from the various components contained in this builder.- 返回:
- the URI components
build
public UriComponents build(boolean encoded)
Variant ofbuild()to create aUriComponentsinstance when components are already fully encoded. This is useful for example if the builder was created viafromUri(URI).- 参数:
encoded- whether the components in this builder are already encoded- 返回:
- the URI components
- 抛出:
IllegalArgumentException- if any of the components contain illegal characters that should have been encoded.
buildAndExpand
public UriComponents buildAndExpand(Map<String,?> uriVariables)
Build aUriComponentsinstance and replaces URI template variables with the values from a map. This is a shortcut method which combines calls tobuild()and thenUriComponents.expand(Map).- 参数:
uriVariables- the map of URI variables- 返回:
- the URI components with expanded values
buildAndExpand
public UriComponents buildAndExpand(Object... uriVariableValues)
Build aUriComponentsinstance and replaces URI template variables with the values from an array. This is a shortcut method which combines calls tobuild()and thenUriComponents.expand(Object...).- 参数:
uriVariableValues- URI variable values- 返回:
- the URI components with expanded values
toUriString
public String toUriString()
Build a URI String. This is a shortcut method which combines calls tobuild(), thenUriComponents.encode()and finallyUriComponents.toUriString().- 从以下版本开始:
- 4.1
- 另请参阅:
UriComponents.toUriString()
uri
public UriComponentsBuilder uri(URI uri)
Initialize components of this builder from components of the given URI.- 参数:
uri- the URI- 返回:
- this UriComponentsBuilder
uriComponents
public UriComponentsBuilder uriComponents(UriComponents uriComponents)
Set or append individual URI components of this builder from the values of the givenUriComponentsinstance.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.- 参数:
uriComponents- the UriComponents to copy from- 返回:
- this UriComponentsBuilder
scheme
public UriComponentsBuilder scheme(String scheme)
Set the URI scheme. The given scheme may contain URI template variables, and may also benullto clear the scheme of this builder.- 参数:
scheme- the URI scheme- 返回:
- this UriComponentsBuilder
schemeSpecificPart
public UriComponentsBuilder schemeSpecificPart(String ssp)
Set the URI scheme-specific-part. When invoked, this method overwrites user-info, host, port, path, andquery.- 参数:
ssp- the URI scheme-specific-part, may contain URI template parameters- 返回:
- this UriComponentsBuilder
userInfo
public UriComponentsBuilder userInfo(String userInfo)
Set the URI user info. The given user info may contain URI template variables, and may also benullto clear the user info of this builder.- 参数:
userInfo- the URI user info- 返回:
- this UriComponentsBuilder
host
public UriComponentsBuilder host(String host)
Set the URI host. The given host may contain URI template variables, and may also benullto clear the host of this builder.- 参数:
host- the URI host- 返回:
- this UriComponentsBuilder
port
public UriComponentsBuilder port(int port)
Set the URI port. Passing-1will clear the port of this builder.- 参数:
port- the URI port- 返回:
- this UriComponentsBuilder
port
public UriComponentsBuilder port(String port)
Set the URI port. Use this method only when the port needs to be parameterized with a URI variable. Otherwise useport(int). Passingnullwill clear the port of this builder.- 参数:
port- the URI port- 返回:
- this UriComponentsBuilder
path
public UriComponentsBuilder path(String path)
Append the given path to the existing path of this builder. The given path may contain URI template variables.- 参数:
path- the URI path- 返回:
- this UriComponentsBuilder
pathSegment
public UriComponentsBuilder pathSegment(String... pathSegments) throws IllegalArgumentException
Append path segments to the existing path. Each path segment may contain URI template variables and should not contain any slashes. Usepath("/")subsequently to ensure a trailing slash.- 参数:
pathSegments- the URI path segments- 返回:
- this UriComponentsBuilder
- 抛出:
IllegalArgumentException
replacePath
public UriComponentsBuilder replacePath(String path)
Set the path of this builder overriding all existing path and path segment values.- 参数:
path- the URI path (anullvalue results in an empty path)- 返回:
- this UriComponentsBuilder
query
public UriComponentsBuilder query(String query)
Append the given query to the existing query of this builder. The given query may contain URI template variables.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:UriComponentsBuilder.fromUriString("/hotels/42") .query("filter={value}") .buildAndExpand("hot&cold");- 参数:
query- the query string- 返回:
- this UriComponentsBuilder
replaceQuery
public UriComponentsBuilder replaceQuery(String query)
Set the query of this builder overriding all existing query parameters.- 参数:
query- the query string; anullvalue removes all query parameters.- 返回:
- this UriComponentsBuilder
queryParam
public UriComponentsBuilder queryParam(String name, Object... values)
Append the given query parameter to the existing query parameters. The given name or any of the values may contain URI template variables. If no values are given, the resulting URI will contain the query parameter name only (i.e.?fooinstead of?foo=bar).- 参数:
name- the query parameter namevalues- the query parameter values- 返回:
- this UriComponentsBuilder
queryParams
public UriComponentsBuilder queryParams(MultiValueMap<String,String> params)
Add the given query parameters.- 参数:
params- the params- 返回:
- this UriComponentsBuilder
- 从以下版本开始:
- 4.0
replaceQueryParam
public UriComponentsBuilder replaceQueryParam(String name, Object... values)
Set the query parameter values overriding all existing query values for the same parameter. If no values are given, the query parameter is removed.- 参数:
name- the query parameter namevalues- the query parameter values- 返回:
- this UriComponentsBuilder
replaceQueryParams
public UriComponentsBuilder replaceQueryParams(MultiValueMap<String,String> params)
Set the query parameter values overriding all existing query values.- 参数:
params- the query parameter name- 返回:
- this UriComponentsBuilder
- 从以下版本开始:
- 4.2
fragment
public UriComponentsBuilder fragment(String fragment)
Set the URI fragment. The given fragment may contain URI template variables, and may also benullto clear the fragment of this builder.- 参数:
fragment- the URI fragment- 返回:
- this UriComponentsBuilder
clone
public Object clone()
Public declaration of Object'sclone()method. Delegates tocloneBuilder().
cloneBuilder
public UriComponentsBuilder cloneBuilder()
Clone thisUriComponentsBuilder.- 返回:
- the cloned
UriComponentsBuilderobject - 从以下版本开始:
- 4.2.7