public class UriTemplate
extends java.lang.Object
implements java.io.Serializable
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.
Modifier and Type | Class and Description |
---|---|
private static class |
UriTemplate.TemplateInfo
Helper to extract variable names and regex for matching to actual URLs.
|
Modifier and Type | Field and Description |
---|---|
private java.util.regex.Pattern |
matchPattern |
private UriComponents |
uriComponents |
private java.lang.String |
uriTemplate |
private java.util.List<java.lang.String> |
variableNames |
Constructor and Description |
---|
UriTemplate(java.lang.String uriTemplate)
Construct a new
UriTemplate with the given URI String. |
Modifier and Type | Method and Description |
---|---|
java.net.URI |
expand(java.util.Map<java.lang.String,?> uriVariables)
Given the Map of variables, expands this template into a URI.
|
java.net.URI |
expand(java.lang.Object... uriVariableValues)
Given an array of variables, expand this template into a full URI.
|
java.util.List<java.lang.String> |
getVariableNames()
Return the names of the variables in the template, in order.
|
java.util.Map<java.lang.String,java.lang.String> |
match(java.lang.String uri)
Match the given URI to a map of variable values.
|
boolean |
matches(java.lang.String uri)
Indicate whether the given URI matches this template.
|
java.lang.String |
toString() |
private final java.lang.String uriTemplate
private final UriComponents uriComponents
private final java.util.List<java.lang.String> variableNames
private final java.util.regex.Pattern matchPattern
public UriTemplate(java.lang.String uriTemplate)
UriTemplate
with the given URI String.uriTemplate
- the URI template stringpublic java.util.List<java.lang.String> getVariableNames()
public java.net.URI expand(java.util.Map<java.lang.String,?> uriVariables)
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
uriVariables
- the map of URI variablesjava.lang.IllegalArgumentException
- if uriVariables
is null
;
or if it does not contain values for all the variable namespublic java.net.URI expand(java.lang.Object... uriVariableValues)
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
uriVariableValues
- the array of URI variablesjava.lang.IllegalArgumentException
- if uriVariables
is null
or if it does not contain sufficient variablespublic boolean matches(java.lang.String uri)
uri
- the URI to match totrue
if it matches; false
otherwisepublic java.util.Map<java.lang.String,java.lang.String> match(java.lang.String 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}
uri
- the URI to match topublic java.lang.String toString()
toString
in class java.lang.Object