Class UriUtils
java.lang.Object
org.springframework.web.util.UriUtils
Utility methods for URI encoding and decoding based on RFC 3986.
There are two types of encode methods:
"encodeXyz"-- these encode a specific URI component (for example, path, query) by percent encoding illegal characters, which includes non-US-ASCII characters, and also characters that are otherwise illegal within the given URI component type, as defined in RFC 3986. The effect of this method, with regards to encoding, is comparable to using the multi-argument constructor ofURI."encode"and"encodeUriVariables"-- these can be used to encode URI variable values by percent encoding all characters that are either illegal, or have any reserved meaning, anywhere within a URI.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringDecode the given encoded URI component value by replacing each "%xy" sequence with a hexadecimal representation of the character in the specified character encoding, leaving other characters unmodified.static StringDecode the given encoded URI component value by replacing each "%xy" sequence with a hexadecimal representation of the character in the specified character encoding, leaving other characters unmodified.static StringVariant ofencode(String, Charset)with a String charset.static StringEncode all characters that are either illegal, or have any reserved meaning, anywhere within a URI, as defined in RFC 3986.static StringencodeAuthority(String authority, String encoding) Encode the given URI authority with the given encoding.static StringencodeAuthority(String authority, Charset charset) Encode the given URI authority with the given encoding.static StringencodeFragment(String fragment, String encoding) Encode the given URI fragment with the given encoding.static StringencodeFragment(String fragment, Charset charset) Encode the given URI fragment with the given encoding.static StringencodeHost(String host, String encoding) Encode the given URI host with the given encoding.static StringencodeHost(String host, Charset charset) Encode the given URI host with the given encoding.static StringencodePath(String path, String encoding) Encode the given URI path with the given encoding.static StringencodePath(String path, Charset charset) Encode the given URI path with the given encoding.static StringencodePathSegment(String segment, String encoding) Encode the given URI path segment with the given encoding.static StringencodePathSegment(String segment, Charset charset) Encode the given URI path segment with the given encoding.static StringencodePort(String port, String encoding) Encode the given URI port with the given encoding.static StringencodePort(String port, Charset charset) Encode the given URI port with the given encoding.static StringencodeQuery(String query, String encoding) Encode the given URI query with the given encoding.static StringencodeQuery(String query, Charset charset) Encode the given URI query with the given encoding.static StringencodeQueryParam(String queryParam, String encoding) Encode the given URI query parameter with the given encoding.static StringencodeQueryParam(String queryParam, Charset charset) Encode the given URI query parameter with the given encoding.static MultiValueMap<String, String> encodeQueryParams(MultiValueMap<String, String> params) Encode the query parameters from the givenMultiValueMapwith UTF-8.static StringencodeScheme(String scheme, String encoding) Encode the given URI scheme with the given encoding.static StringencodeScheme(String scheme, Charset charset) Encode the given URI scheme with the given encoding.static Object[]encodeUriVariables(@Nullable Object... uriVariables) Convenience method to applyencode(String, Charset)to all given URI variable values.encodeUriVariables(Map<String, ? extends @Nullable Object> uriVariables) Convenience method to applyencode(String, Charset)to all given URI variable values.static StringencodeUserInfo(String userInfo, String encoding) Encode the given URI user info with the given encoding.static StringencodeUserInfo(String userInfo, Charset charset) Encode the given URI user info with the given encoding.extractFileExtension(String path) Extract the file extension from the given URI path.
-
Constructor Details
-
UriUtils
public UriUtils()
-
-
Method Details
-
encodeScheme
-
encodeScheme
-
encodeAuthority
-
encodeAuthority
-
encodeUserInfo
-
encodeUserInfo
-
encodeHost
-
encodeHost
-
encodePort
-
encodePort
-
encodePath
-
encodePath
-
encodePathSegment
-
encodePathSegment
-
encodeQuery
-
encodeQuery
-
encodeQueryParam
-
encodeQueryParam
-
encodeQueryParams
Encode the query parameters from the givenMultiValueMapwith UTF-8.This can be used with
UriComponentsBuilder.queryParams(MultiValueMap)when building a URI from an already encoded template.MultiValueMap<String, String> params = new LinkedMultiValueMap<>(2); // add to params... ServletUriComponentsBuilder.fromCurrentRequest() .queryParams(UriUtils.encodeQueryParams(params)) .build(true) .toUriString();- Parameters:
params- the parameters to encode- Returns:
- a new
MultiValueMapwith the encoded names and values - Since:
- 5.2.3
-
encodeFragment
-
encodeFragment
-
encode
Variant ofencode(String, Charset)with a String charset.- Parameters:
source- the String to be encodedencoding- the character encoding to encode to- Returns:
- the encoded String
-
encode
Encode all characters that are either illegal, or have any reserved meaning, anywhere within a URI, as defined in RFC 3986. This is useful to ensure that the given String will be preserved as-is and will not have any impact on the structure or meaning of the URI.- Parameters:
source- the String to be encodedcharset- the character encoding to encode to- Returns:
- the encoded String
- Since:
- 5.0
-
encodeUriVariables
-
encodeUriVariables
Convenience method to applyencode(String, Charset)to all given URI variable values.- Parameters:
uriVariables- the URI variable values to be encoded- Returns:
- the encoded String
- Since:
- 5.0
-
decode
Decode the given encoded URI component value by replacing each "%xy" sequence with a hexadecimal representation of the character in the specified character encoding, leaving other characters unmodified.- Parameters:
source- the encoded URI component valueencoding- the character encoding to use to decode the "%xy" sequences- Returns:
- the decoded value
- Throws:
IllegalArgumentException- if the given source contains invalid encoded sequences- See Also:
-
decode
Decode the given encoded URI component value by replacing each "%xy" sequence with a hexadecimal representation of the character in the specified character encoding, leaving other characters unmodified.- Parameters:
source- the encoded URI component valuecharset- the character encoding to use to decode the "%xy" sequences- Returns:
- the decoded value
- Throws:
IllegalArgumentException- if the given source contains invalid encoded sequences- Since:
- 5.0
- See Also:
-
extractFileExtension
-