diff --git a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java index 95e8c2d6b7..43d6094396 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java @@ -197,6 +197,7 @@ public class UriTemplate implements Serializable { if (c == '{') { level++; if (level == 1) { + // start of URI variable pattern.append(quote(builder)); builder = new StringBuilder(); continue; @@ -205,6 +206,7 @@ public class UriTemplate implements Serializable { else if (c == '}') { level--; if (level == 0) { + // end of URI variable String variable = builder.toString(); int idx = variable.indexOf(':'); if (idx == -1) { @@ -227,14 +229,11 @@ public class UriTemplate implements Serializable { continue; } } - if (i + 1 == uriTemplate.length()) { - if (c != '/') { - builder.append(c); - } - pattern.append(quote(builder)); - } builder.append(c); } + if (builder.length() > 0) { + pattern.append(quote(builder)); + } return new TemplateInfo(variableNames, Pattern.compile(pattern.toString())); } diff --git a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java index 1e0d6c2316..a047af03eb 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -185,6 +185,14 @@ public class UriTemplateTests { assertTrue(template.matches("/search?query=foo#bar")); } + // SPR-13705 + + @Test + public void matchesWithSlashAtTheEnd() { + UriTemplate uriTemplate = new UriTemplate("/test/"); + assertTrue(uriTemplate.matches("/test/")); + } + @Test public void expandWithDollar() { UriTemplate template = new UriTemplate("/{a}");