Top Description Inners Fields Constructors Methods
org.springframework.web.util

public Class UriTemplate

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@SuppressWarnings:serial
Imports
java.io.Serializable, java.net.URI, java.util.ArrayList, .Collections, .List, .Map, java.util.regex.Matcher, .Pattern, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils

Representation of a URI template that can be expanded with URI variables via expand(Map), expand(Object[]), or matched to a URL via match(String). This class is designed to be thread-safe and reusable, and allows any number of expand or match calls.

Note

this class uses UriComponentsBuilder internally to expand URI templates, and is merely a shortcut for already prepared URI templates. For more dynamic preparation and extra flexibility, e.g. around URI encoding, consider using UriComponentsBuilder or the higher level DefaultUriBuilderFactory which adds several encoding modes on top of UriComponentsBuilder. See the reference docs for further details.

Authors
Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
UriTemplate.TemplateInfo

Helper to extract variable names and regex for matching to actual URLs.

Field Summary

Modifier and TypeField and Description
private final Pattern
private final UriComponents
private final String
private final List<String>

Constructor Summary

AccessConstructor and Description
public
UriTemplate(String
the URI template string
uriTemplate
)

Construct a new UriTemplate with the given URI String.

Method Summary

Modifier and TypeMethod and Description
public URI

Returns:

the expanded URI
expand
(Map<String, ?>
the map of URI variables
uriVariables
)

Given the Map of variables, expands this template into a URI.

public URI

Returns:

the expanded URI
expand
(Object...
the array of URI variables
uriVariableValues
)

Given an array of variables, expand this template into a full URI.

public List<String>

Returns:

the template variable names
getVariableNames
()

Return the names of the variables in the template, in order.

public Map<String, String>

Returns:

a map of variable values
match
(String
the URI to match to
uri
)

Match the given URI to a map of variable values.

public boolean

Returns:

true if it matches; false otherwise
matches
(String
the URI to match to
uri
)

Indicate whether the given URI matches this template.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

matchPatternback to summary
private final Pattern matchPattern
uriComponentsback to summary
private final UriComponents uriComponents
uriTemplateback to summary
private final String uriTemplate
variableNamesback to summary
private final List<String> variableNames

Constructor Detail

UriTemplateback to summary
public UriTemplate(String uriTemplate)

Construct a new UriTemplate with the given URI String.

Parameters
uriTemplate:String

the URI template string

Method Detail

expandback to summary
public URI expand(Map<String, ?> uriVariables)

Given the Map of variables, expands this template into a URI. The Map keys represent variable names, the Map values variable values. The order of variables is not significant.

Example:

UriTemplate template = new UriTemplate("https://example.com/hotels/{hotel}/bookings/{booking}");
Map<String, String> uriVariables = new HashMap<String, String>();
uriVariables.put("booking", "42");
uriVariables.put("hotel", "Rest & Relax");
System.out.println(template.expand(uriVariables));
will print:
https://example.com/hotels/Rest%20%26%20Relax/bookings/42
Parameters
uriVariables:Map<String, ?>

the map of URI variables

Returns:URI

the expanded URI

Exceptions
IllegalArgumentException:
if uriVariables is null; or if it does not contain values for all the variable names
expandback to summary
public URI expand(Object... uriVariableValues)

Given an array of variables, expand this template into a full URI. The array represent variable values. The order of variables is significant.

Example:

UriTemplate template = new UriTemplate("https://example.com/hotels/{hotel}/bookings/{booking}");
System.out.println(template.expand("Rest & Relax", 42));
will print:
https://example.com/hotels/Rest%20%26%20Relax/bookings/42
Parameters
uriVariableValues:Object[]

the array of URI variables

Returns:URI

the expanded URI

Exceptions
IllegalArgumentException:
if uriVariables is null or if it does not contain sufficient variables
getVariableNamesback to summary
public List<String> getVariableNames()

Return the names of the variables in the template, in order.

Returns:List<String>

the template variable names

matchback to summary
public Map<String, String> match(String uri)

Match the given URI to a map of variable values. Keys in the returned map are variable names, values are variable values, as occurred in the given URI.

Example:

UriTemplate template = new UriTemplate("https://example.com/hotels/{hotel}/bookings/{booking}");
System.out.println(template.match("https://example.com/hotels/1/bookings/42"));
will print:
{hotel=1, booking=42}
Parameters
uri:String

the URI to match to

Returns:Map<String, String>

a map of variable values

matchesback to summary
public boolean matches(String uri)

Indicate whether the given URI matches this template.

Parameters
uri:String

the URI to match to

Returns:boolean

true if it matches; false otherwise

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.springframework.web.util back to summary

private final Class UriTemplate.TemplateInfo

extends Object
Class Inheritance

Helper to extract variable names and regex for matching to actual URLs.

Field Summary

Modifier and TypeField and Description
private final Pattern
private final List<String>

Constructor Summary

AccessConstructor and Description
private
TemplateInfo(List<String> vars, Pattern pattern)

Method Summary

Modifier and TypeMethod and Description
public Pattern
public List<String>
public static UriTemplate.TemplateInfo
parse(String uriTemplate)

private static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

patternback to summary
private final Pattern pattern
variableNamesback to summary
private final List<String> variableNames

Constructor Detail

TemplateInfoback to summary
private TemplateInfo(List<String> vars, Pattern pattern)

Method Detail

getMatchPatternback to summary
public Pattern getMatchPattern()
getVariableNamesback to summary
public List<String> getVariableNames()
parseback to summary
public static UriTemplate.TemplateInfo parse(String uriTemplate)
quoteback to summary
private static String quote(StringBuilder builder)