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 4cf28d5ef9e..708e888706c 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 @@ -629,18 +629,31 @@ public class MvcUriComponentsBuilder { } private static String resolveEmbeddedValue(String value) { - WebApplicationContext webApplicationContext = getWebApplicationContext(); - if (webApplicationContext != null && - webApplicationContext.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) { - EmbeddedValueResolver embeddedValueResolver = new EmbeddedValueResolver(cbf); - String resolvedEmbeddedValue = embeddedValueResolver.resolveStringValue(value); - if (resolvedEmbeddedValue != null) { - return resolvedEmbeddedValue; + if (hasPlaceholderOrExpression(value)) { + WebApplicationContext wac = getWebApplicationContext(); + if (wac != null && wac.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) { + EmbeddedValueResolver valueResolver = new EmbeddedValueResolver(cbf); + String resolvedValue = valueResolver.resolveStringValue(value); + if (resolvedValue != null) { + return resolvedValue; + } } } return value; } + private static boolean hasPlaceholderOrExpression(String value) { + char prev = 0; + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + if (c == '{' && (prev == '$' || prev == '#')) { + return true; + } + prev = c; + } + return false; + } + private static @Nullable WebApplicationContext getWebApplicationContext() { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes == null) { 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 15d28092ffc..a52bcb94c5b 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 @@ -335,20 +335,18 @@ public class MvcUriComponentsBuilderTests { assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo"); } - @Test + @Test // gh-35348 void fromMethodNameConfigurablePathSpEL() { try { System.setProperty("customMapping", "custom"); StandardEnvironment environment = new StandardEnvironment(); initWebApplicationContext(WebConfig.class, environment); - UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, - "methodWithConfigurableMappingThroughSpEL", "1").build(); - assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo"); + UriComponents uric = fromMethodName(ControllerWithMethods.class, "methodWithSpEL", "1").build(); + assertThat(uric.toUriString()).isEqualTo("http://localhost/something/custom/1/foo"); } finally { System.clearProperty("customMapping"); } - } @Test @@ -721,7 +719,7 @@ public class MvcUriComponentsBuilderTests { } @RequestMapping("/#{systemProperties.customMapping}/{id}/foo") - HttpEntity methodWithConfigurableMappingThroughSpEL(@PathVariable String id) { + HttpEntity methodWithSpEL(@PathVariable String id) { return null; } }