Top Description Constructors Methods
org.springframework.web.util

public abstract Class UriUtils

extends Object
Class Inheritance
Imports
java.net.URI, java.nio.charset.Charset, .StandardCharsets, java.util.Arrays, .List, .Map, org.springframework.lang.Nullable, org.springframework.util.CollectionUtils, .LinkedMultiValueMap, .MultiValueMap, .StringUtils

Utility methods for URI encoding and decoding based on RFC 3986.

There are two types of encode methods:

Authors
Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
Since
3.0
See Also
RFC 3986

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the decoded value
decode
(String
the encoded String
source
,
String
the character encoding to use
encoding
)

Decode the given encoded URI component.

public static String

Returns:

the decoded value
decode
(String
the encoded String
source
,
Charset
the character encoding to use
charset
)

Decode the given encoded URI component.

public static String

Returns:

the encoded String
encode
(String
the String to be encoded
source
,
String
the character encoding to encode to
encoding
)

Variant of encode(String, Charset) with a String charset.

public static String

Returns:

the encoded String
encode
(String
the String to be encoded
source
,
Charset
the character encoding to encode to
charset
)

Encode all characters that are either illegal, or have any reserved meaning, anywhere within a URI, as defined in RFC 3986.

private static String
private static String
public static String

Returns:

the encoded authority
encodeAuthority
(String
the authority to be encoded
authority
,
String
the character encoding to encode to
encoding
)

Encode the given URI authority with the given encoding.

public static String

Returns:

the encoded authority
encodeAuthority
(String
the authority to be encoded
authority
,
Charset
the character encoding to encode to
charset
)

Encode the given URI authority with the given encoding.

public static String

Returns:

the encoded fragment
encodeFragment
(String
the fragment to be encoded
fragment
,
String
the character encoding to encode to
encoding
)

Encode the given URI fragment with the given encoding.

public static String

Returns:

the encoded fragment
encodeFragment
(String
the fragment to be encoded
fragment
,
Charset
the character encoding to encode to
charset
)

Encode the given URI fragment with the given encoding.

public static String

Returns:

the encoded host
encodeHost
(String
the host to be encoded
host
,
String
the character encoding to encode to
encoding
)

Encode the given URI host with the given encoding.

public static String

Returns:

the encoded host
encodeHost
(String
the host to be encoded
host
,
Charset
the character encoding to encode to
charset
)

Encode the given URI host with the given encoding.

public static String

Returns:

the encoded path
encodePath
(String
the path to be encoded
path
,
String
the character encoding to encode to
encoding
)

Encode the given URI path with the given encoding.

public static String

Returns:

the encoded path
encodePath
(String
the path to be encoded
path
,
Charset
the character encoding to encode to
charset
)

Encode the given URI path with the given encoding.

public static String

Returns:

the encoded segment
encodePathSegment
(String
the segment to be encoded
segment
,
String
the character encoding to encode to
encoding
)

Encode the given URI path segment with the given encoding.

public static String

Returns:

the encoded segment
encodePathSegment
(String
the segment to be encoded
segment
,
Charset
the character encoding to encode to
charset
)

Encode the given URI path segment with the given encoding.

public static String

Returns:

the encoded port
encodePort
(String
the port to be encoded
port
,
String
the character encoding to encode to
encoding
)

Encode the given URI port with the given encoding.

public static String

Returns:

the encoded port
encodePort
(String
the port to be encoded
port
,
Charset
the character encoding to encode to
charset
)

Encode the given URI port with the given encoding.

public static String

Returns:

the encoded query
encodeQuery
(String
the query to be encoded
query
,
String
the character encoding to encode to
encoding
)

Encode the given URI query with the given encoding.

public static String

Returns:

the encoded query
encodeQuery
(String
the query to be encoded
query
,
Charset
the character encoding to encode to
charset
)

Encode the given URI query with the given encoding.

public static String

Returns:

the encoded query parameter
encodeQueryParam
(String
the query parameter to be encoded
queryParam
,
String
the character encoding to encode to
encoding
)

Encode the given URI query parameter with the given encoding.

public static String

Returns:

the encoded query parameter
encodeQueryParam
(String
the query parameter to be encoded
queryParam
,
Charset
the character encoding to encode to
charset
)

Encode the given URI query parameter with the given encoding.

public static MultiValueMap<String, String>

Returns:

a new MultiValueMap with the encoded names and values
encodeQueryParams
(MultiValueMap<String, String>
the parameters to encode
params
)

Encode the query parameters from the given MultiValueMap with UTF-8.

public static String

Returns:

the encoded scheme
encodeScheme
(String
the scheme to be encoded
scheme
,
String
the character encoding to encode to
encoding
)

Encode the given URI scheme with the given encoding.

public static String

Returns:

the encoded scheme
encodeScheme
(String
the scheme to be encoded
scheme
,
Charset
the character encoding to encode to
charset
)

Encode the given URI scheme with the given encoding.

public static Map<String, String>

Returns:

the encoded String
encodeUriVariables
(Map<String, ?>
the URI variable values to be encoded
uriVariables
)

Convenience method to apply encode(String, Charset) to all given URI variable values.

public static Object[]

Returns:

the encoded String
encodeUriVariables
(Object...
the URI variable values to be encoded
uriVariables
)

Convenience method to apply encode(String, Charset) to all given URI variable values.

public static String

Returns:

the encoded user info
encodeUserInfo
(String
the user info to be encoded
userInfo
,
String
the character encoding to encode to
encoding
)

Encode the given URI user info with the given encoding.

public static String

Returns:

the encoded user info
encodeUserInfo
(String
the user info to be encoded
userInfo
,
Charset
the character encoding to encode to
charset
)

Encode the given URI user info with the given encoding.

public static String

Returns:

the extracted file extension (e.g. "html")
extractFileExtension
(String
the URI path (e.g. "/products/index.html")
path
)

Extract the file extension from the given URI path.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

UriUtilsback to summary
public UriUtils()

Method Detail

decodeback to summary
public static String decode(String source, String encoding)

Decode the given encoded URI component.

See StringUtils#uriDecode(String, Charset) for the decoding rules.

Parameters
source:String

the encoded String

encoding:String

the character encoding to use

Returns:String

the decoded value

Exceptions
IllegalArgumentException:
when the given source contains invalid encoded sequences
See Also
StringUtils#uriDecode(String, Charset), java.net.URLDecoder#decode(String, String)
decodeback to summary
public static String decode(String source, Charset charset)

Decode the given encoded URI component.

See StringUtils#uriDecode(String, Charset) for the decoding rules.

Parameters
source:String

the encoded String

charset:Charset

the character encoding to use

Returns:String

the decoded value

Exceptions
IllegalArgumentException:
when the given source contains invalid encoded sequences
Since
5.0
See Also
StringUtils#uriDecode(String, Charset), java.net.URLDecoder#decode(String, String)
encodeback to summary
public static String encode(String source, String encoding)

Variant of encode(String, Charset) with a String charset.

Parameters
source:String

the String to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded String

encodeback to summary
public static String encode(String source, Charset charset)

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:String

the String to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded String

Since
5.0
encodeback to summary
private static String encode(String scheme, String encoding, HierarchicalUriComponents.Type type)
encodeback to summary
private static String encode(String scheme, Charset charset, HierarchicalUriComponents.Type type)
encodeAuthorityback to summary
public static String encodeAuthority(String authority, String encoding)

Encode the given URI authority with the given encoding.

Parameters
authority:String

the authority to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded authority

encodeAuthorityback to summary
public static String encodeAuthority(String authority, Charset charset)

Encode the given URI authority with the given encoding.

Parameters
authority:String

the authority to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded authority

Since
5.0
encodeFragmentback to summary
public static String encodeFragment(String fragment, String encoding)

Encode the given URI fragment with the given encoding.

Parameters
fragment:String

the fragment to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded fragment

encodeFragmentback to summary
public static String encodeFragment(String fragment, Charset charset)

Encode the given URI fragment with the given encoding.

Parameters
fragment:String

the fragment to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded fragment

Since
5.0
encodeHostback to summary
public static String encodeHost(String host, String encoding)

Encode the given URI host with the given encoding.

Parameters
host:String

the host to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded host

encodeHostback to summary
public static String encodeHost(String host, Charset charset)

Encode the given URI host with the given encoding.

Parameters
host:String

the host to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded host

Since
5.0
encodePathback to summary
public static String encodePath(String path, String encoding)

Encode the given URI path with the given encoding.

Parameters
path:String

the path to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded path

encodePathback to summary
public static String encodePath(String path, Charset charset)

Encode the given URI path with the given encoding.

Parameters
path:String

the path to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded path

Since
5.0
encodePathSegmentback to summary
public static String encodePathSegment(String segment, String encoding)

Encode the given URI path segment with the given encoding.

Parameters
segment:String

the segment to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded segment

encodePathSegmentback to summary
public static String encodePathSegment(String segment, Charset charset)

Encode the given URI path segment with the given encoding.

Parameters
segment:String

the segment to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded segment

Since
5.0
encodePortback to summary
public static String encodePort(String port, String encoding)

Encode the given URI port with the given encoding.

Parameters
port:String

the port to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded port

encodePortback to summary
public static String encodePort(String port, Charset charset)

Encode the given URI port with the given encoding.

Parameters
port:String

the port to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded port

Since
5.0
encodeQueryback to summary
public static String encodeQuery(String query, String encoding)

Encode the given URI query with the given encoding.

Parameters
query:String

the query to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded query

encodeQueryback to summary
public static String encodeQuery(String query, Charset charset)

Encode the given URI query with the given encoding.

Parameters
query:String

the query to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded query

Since
5.0
encodeQueryParamback to summary
public static String encodeQueryParam(String queryParam, String encoding)

Encode the given URI query parameter with the given encoding.

Parameters
queryParam:String

the query parameter to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded query parameter

encodeQueryParamback to summary
public static String encodeQueryParam(String queryParam, Charset charset)

Encode the given URI query parameter with the given encoding.

Parameters
queryParam:String

the query parameter to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded query parameter

Since
5.0
encodeQueryParamsback to summary
public static MultiValueMap<String, String> encodeQueryParams(MultiValueMap<String, String> params)

Encode the query parameters from the given MultiValueMap with 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:MultiValueMap<String, String>

the parameters to encode

Returns:MultiValueMap<String, String>

a new MultiValueMap with the encoded names and values

Since
5.2.3
encodeSchemeback to summary
public static String encodeScheme(String scheme, String encoding)

Encode the given URI scheme with the given encoding.

Parameters
scheme:String

the scheme to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded scheme

encodeSchemeback to summary
public static String encodeScheme(String scheme, Charset charset)

Encode the given URI scheme with the given encoding.

Parameters
scheme:String

the scheme to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded scheme

Since
5.0
encodeUriVariablesback to summary
public static Map<String, String> encodeUriVariables(Map<String, ?> uriVariables)

Convenience method to apply encode(String, Charset) to all given URI variable values.

Parameters
uriVariables:Map<String, ?>

the URI variable values to be encoded

Returns:Map<String, String>

the encoded String

Since
5.0
encodeUriVariablesback to summary
public static Object[] encodeUriVariables(Object... uriVariables)

Convenience method to apply encode(String, Charset) to all given URI variable values.

Parameters
uriVariables:Object[]

the URI variable values to be encoded

Returns:Object[]

the encoded String

Since
5.0
encodeUserInfoback to summary
public static String encodeUserInfo(String userInfo, String encoding)

Encode the given URI user info with the given encoding.

Parameters
userInfo:String

the user info to be encoded

encoding:String

the character encoding to encode to

Returns:String

the encoded user info

encodeUserInfoback to summary
public static String encodeUserInfo(String userInfo, Charset charset)

Encode the given URI user info with the given encoding.

Parameters
userInfo:String

the user info to be encoded

charset:Charset

the character encoding to encode to

Returns:String

the encoded user info

Since
5.0
extractFileExtensionback to summary
public static String extractFileExtension(String path)

Extract the file extension from the given URI path.

Parameters
path:String

the URI path (e.g. "/products/index.html")

Returns:String

the extracted file extension (e.g. "html")

Annotations
@Nullable
Since
4.3.2