SpringMVC url 路径模式替换

Mak*_*Mak 2 spring spring-mvc spring-mvc-test

我在 SpringMVC 中有一个 url 路径模式,如下所示:

/person/{personId}/address/{addressId}
Run Code Online (Sandbox Code Playgroud)

我有 personId = 2 和 addressId = 3 有没有一种简单的方法可以让我生成

/person/2/address/3 
Run Code Online (Sandbox Code Playgroud)

在 SpringMvc 中使用实用方法?

MaV*_*ldo 5

看一下UriTemplate类。您可以从 URL 构造自己的 UriTemplate,然后展开模板变量。

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));
Run Code Online (Sandbox Code Playgroud)