类 UriTemplate
- java.lang.Object
- org.springframework.web.util.UriTemplate
- 所有已实现的接口:
Serializable
public class UriTemplate extends Object implements Serializable
Representation of a URI template that can be expanded with URI variables viaexpand(Map)
,expand(Object[])
, or matched to a URL viamatch(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 usingUriComponentsBuilder
or the higher levelDefaultUriBuilderFactory
which adds several encoding modes on top ofUriComponentsBuilder
. See the reference docs for further details.- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
- 另请参阅:
- 序列化表格
构造器概要
构造器 构造器 说明 UriTemplate(String uriTemplate)
Construct a newUriTemplate
with the given URI String.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 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<String,String>
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()
构造器详细资料
UriTemplate
public UriTemplate(String uriTemplate)
Construct a newUriTemplate
with the given URI String.- 参数:
uriTemplate
- the URI template string
方法详细资料
getVariableNames
public List<String> getVariableNames()
Return the names of the variables in the template, in order.- 返回:
- the template variable names
expand
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
- 参数:
uriVariables
- the map of URI variables- 返回:
- the expanded URI
- 抛出:
IllegalArgumentException
- ifuriVariables
isnull
; or if it does not contain values for all the variable names
expand
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
- 参数:
uriVariableValues
- the array of URI variables- 返回:
- the expanded URI
- 抛出:
IllegalArgumentException
- ifuriVariables
isnull
or if it does not contain sufficient variables
matches
public boolean matches(String uri)
Indicate whether the given URI matches this template.- 参数:
uri
- the URI to match to- 返回:
true
if it matches;false
otherwise
match
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}
- 参数:
uri
- the URI to match to- 返回:
- a map of variable values