diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java b/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java index b997a657c5..a7605e710c 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java +++ b/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java @@ -125,8 +125,9 @@ public class UriTemplate { StringBuffer buffer = new StringBuffer(); int i = 0; while (matcher.find()) { - String uriVariable = uriVariableValues[i++].toString(); - matcher.appendReplacement(buffer, Matcher.quoteReplacement(uriVariable)); + Object uriVariable = uriVariableValues[i++]; + String replacement = Matcher.quoteReplacement(uriVariable != null ? uriVariable.toString() : ""); + matcher.appendReplacement(buffer, replacement); } matcher.appendTail(buffer); return encodeUri(buffer.toString()); diff --git a/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateTests.java b/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateTests.java index 0f83e8b8e2..4bdf011557 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateTests.java +++ b/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateTests.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.net.URI; import java.util.Collections; import java.util.EnumSet; +import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -82,6 +83,21 @@ public class RestTemplateTests { verifyMocks(); } + + @Test + public void varArgsNullTemplateVariable() throws Exception { + expect(requestFactory.createRequest(new URI("http://example.com/-foo"), HttpMethod.GET)) + .andReturn(request); + expect(request.execute()).andReturn(response); + expect(errorHandler.hasError(response)).andReturn(false); + response.close(); + + replayMocks(); + + template.execute("http://example.com/{first}-{last}", HttpMethod.GET, null, null, null, "foo"); + + verifyMocks(); + } @Test public void mapTemplateVariables() throws Exception { @@ -99,6 +115,24 @@ public class RestTemplateTests { verifyMocks(); } + @Test + public void mapNullTemplateVariable() throws Exception { + expect(requestFactory.createRequest(new URI("http://example.com/-foo"), HttpMethod.GET)) + .andReturn(request); + expect(request.execute()).andReturn(response); + expect(errorHandler.hasError(response)).andReturn(false); + response.close(); + + replayMocks(); + + Map vars = new HashMap(2); + vars.put("first", null); + vars.put("last", "foo"); + template.execute("http://example.com/{first}-{last}", HttpMethod.GET, null, null, vars); + + verifyMocks(); + } + @Test public void errorHandling() throws Exception { expect(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET)).andReturn(request);