spring-framework / org.springframework.web.util / UriTemplate

UriTemplate

open class UriTemplate : Serializable

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.

This class is designed to be thread-safe and reusable, allowing for any number of expand or match calls.

Author
Arjen Poutsma

Author
Juergen Hoeller

Author
Rossen Stoyanchev

Since
3.0

Constructors

<init>

UriTemplate(uriTemplate: String)

Construct a new UriTemplate with the given URI String.

Functions

expand

open fun expand(uriVariables: MutableMap<String, *>): URI

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", "Rest & Relax"); System.out.println(template.expand(uriVariables)); 
will print: http://example.com/hotels/Rest%20%26%20Relax/bookings/42

open fun expand(vararg uriVariableValues: Any): URI

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("Rest & Relax", 42)); 
will print: http://example.com/hotels/Rest%20%26%20Relax/bookings/42

getVariableNames

open fun getVariableNames(): MutableList<String>

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

match

open fun match(uri: String): MutableMap<String, String>

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}

matches

open fun matches(uri: String): Boolean

Indicate whether the given URI matches this template.

toString

open fun toString(): String