From ad4c8795ae13c94f942526765b5cf36ac6c3ae9b Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 20 May 2015 17:06:47 -0400 Subject: [PATCH] Include only path in Issue: SPR-13045 --- .../mvc/method/annotation/MvcUriComponentsBuilder.java | 8 +++++++- .../method/annotation/MvcUriComponentsBuilderTests.java | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java index 22274cee60..ad132b676a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java @@ -740,7 +740,7 @@ public class MvcUriComponentsBuilder { public MethodArgumentBuilder(UriComponentsBuilder baseUrl, Class controllerType, Method method) { Assert.notNull(controllerType, "'controllerType' is required"); Assert.notNull(method, "'method' is required"); - this.baseUrl = baseUrl; + this.baseUrl = (baseUrl != null ? baseUrl : initBaseUrl()); this.controllerType = controllerType; this.method = method; this.argumentValues = new Object[method.getParameterTypes().length]; @@ -749,11 +749,17 @@ public class MvcUriComponentsBuilder { } } + private static UriComponentsBuilder initBaseUrl() { + UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentServletMapping(); + return UriComponentsBuilder.fromPath(builder.build().getPath()); + } + /** * @deprecated as of 4.2 deprecated in favor of alternative constructors * that accept the controllerType. */ @Deprecated + @SuppressWarnings("unused") public MethodArgumentBuilder(Method method) { this(method.getDeclaringClass(), method); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java index 7aa613743f..a1f73c079b 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java @@ -312,7 +312,7 @@ public class MvcUriComponentsBuilderTests { String mappingName = "PAC#getAddressesForCountry"; String url = MvcUriComponentsBuilder.fromMappingName(mappingName).arg(0, "DE").buildAndExpand(123); - assertEquals("http://example.org:9999/base/people/123/addresses/DE", url); + assertEquals("/base/people/123/addresses/DE", url); } @Test