Skip to content

Commit 43c13ca

Browse files
committed
Polishing contribution
Closes gh-35348
1 parent 17cf2ad commit 43c13ca

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,18 +629,31 @@ private static CompositeUriComponentsContributor getUriComponentsContributor() {
629629
}
630630

631631
private static String resolveEmbeddedValue(String value) {
632-
WebApplicationContext webApplicationContext = getWebApplicationContext();
633-
if (webApplicationContext != null &&
634-
webApplicationContext.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) {
635-
EmbeddedValueResolver embeddedValueResolver = new EmbeddedValueResolver(cbf);
636-
String resolvedEmbeddedValue = embeddedValueResolver.resolveStringValue(value);
637-
if (resolvedEmbeddedValue != null) {
638-
return resolvedEmbeddedValue;
632+
if (hasPlaceholderOrExpression(value)) {
633+
WebApplicationContext wac = getWebApplicationContext();
634+
if (wac != null && wac.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) {
635+
EmbeddedValueResolver valueResolver = new EmbeddedValueResolver(cbf);
636+
String resolvedValue = valueResolver.resolveStringValue(value);
637+
if (resolvedValue != null) {
638+
return resolvedValue;
639+
}
639640
}
640641
}
641642
return value;
642643
}
643644

645+
private static boolean hasPlaceholderOrExpression(String value) {
646+
char prev = 0;
647+
for (int i = 0; i < value.length(); i++) {
648+
char c = value.charAt(i);
649+
if (c == '{' && (prev == '$' || prev == '#')) {
650+
return true;
651+
}
652+
prev = c;
653+
}
654+
return false;
655+
}
656+
644657
private static @Nullable WebApplicationContext getWebApplicationContext() {
645658
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
646659
if (requestAttributes == null) {

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,20 +335,18 @@ void fromMethodNameConfigurablePath() {
335335
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
336336
}
337337

338-
@Test
338+
@Test // gh-35348
339339
void fromMethodNameConfigurablePathSpEL() {
340340
try {
341341
System.setProperty("customMapping", "custom");
342342
StandardEnvironment environment = new StandardEnvironment();
343343
initWebApplicationContext(WebConfig.class, environment);
344-
UriComponents uriComponents = fromMethodName(ControllerWithMethods.class,
345-
"methodWithConfigurableMappingThroughSpEL", "1").build();
346-
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
344+
UriComponents uric = fromMethodName(ControllerWithMethods.class, "methodWithSpEL", "1").build();
345+
assertThat(uric.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
347346
}
348347
finally {
349348
System.clearProperty("customMapping");
350349
}
351-
352350
}
353351

354352
@Test
@@ -721,7 +719,7 @@ HttpEntity<Void> methodWithConfigurableMapping(@PathVariable String id) {
721719
}
722720

723721
@RequestMapping("/#{systemProperties.customMapping}/{id}/foo")
724-
HttpEntity<Void> methodWithConfigurableMappingThroughSpEL(@PathVariable String id) {
722+
HttpEntity<Void> methodWithSpEL(@PathVariable String id) {
725723
return null;
726724
}
727725
}

0 commit comments

Comments
 (0)