public class

UriTemplate

extends Object
implements Serializable
java.lang.Object
   ↳ org.springframework.web.util.UriTemplate

Class Overview

Represents a URI template. A URI template is a URI-like String that contains variables enclosed by braces ({, }), which can be expanded to produce an actual URI.

See expand(Map), expand(Object[]), and match(String) for example usages.

See Also

Summary

Public Constructors
UriTemplate(String uriTemplate)
Construct a new UriTemplate with the given URI String.
Public Methods
URI expand(Object... uriVariableValues)
Given an array of variables, expand this template into a full URI.
URI expand(Map<String, ?> uriVariables)
Given the Map of variables, expands this template into a URI.
List<String> getVariableNames()
Return the names of the variables in the template, in order.
Map<StringString> match(String uri)
Match the given URI to a map of variable values.
boolean matches(String uri)
Indicate whether the given URI matches this template.
String toString()
Protected Methods
URI encodeUri(String uri)
Encodes the given String as URL.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public UriTemplate (String uriTemplate)

Construct a new UriTemplate with the given URI String.

Parameters
uriTemplate the URI template string

Public Methods

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("http://example.com/hotels/{hotel}/bookings/{booking}");
 System.out.println(template.expand("1", "42));
 
will print:
http://example.com/hotels/1/bookings/42

Parameters
uriVariableValues the array of URI variables
Returns
  • the expanded URI
Throws
IllegalArgumentException if uriVariables is null or if it does not contain sufficient variables

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("http://example.com/hotels/{hotel}/bookings/{booking}");
 Map<String, String> uriVariables = new HashMap<String, String>();
 uriVariables.put("booking", "42");
 uriVariables.put("hotel", "1");
 System.out.println(template.expand(uriVariables));
 
will print:
http://example.com/hotels/1/bookings/42

Parameters
uriVariables the map of URI variables
Returns
  • the expanded URI
Throws
IllegalArgumentException if uriVariables is null; or if it does not contain values for all the variable names

public List<String> getVariableNames ()

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

Returns
  • the template variable names

public Map<StringString> 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("http://example.com/hotels/{hotel}/bookings/{booking}");
 System.out.println(template.match("http://example.com/hotels/1/bookings/42"));
 
will print:
{hotel=1, booking=42}

Parameters
uri the URI to match to
Returns
  • a map of variable values

public boolean matches (String uri)

Indicate whether the given URI matches this template.

Parameters
uri the URI to match to
Returns
  • true if it matches; false otherwise

public String toString ()

Protected Methods

protected URI encodeUri (String uri)

Encodes the given String as URL.

Defaults to encodeUri(String, String).

Parameters
uri the URI to encode
Returns
  • the encoded URI