Skip to content

Commit 5d6d653

Browse files
committed
Use MethodInvocationInfo class loader in case of core JDK interface type
Closes gh-30210 (cherry picked from commit ce2689e)
1 parent 9868e88 commit 5d6d653

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,12 @@ private static <T> T initProxy(
776776
}
777777

778778
else if (controllerType.isInterface()) {
779-
return (T) Proxy.newProxyInstance(controllerType.getClassLoader(),
780-
new Class<?>[] {controllerType, MethodInvocationInfo.class},
781-
interceptor);
779+
ClassLoader classLoader = controllerType.getClassLoader();
780+
if (classLoader == null) { // JDK interface type from bootstrap loader
781+
classLoader = MethodInvocationInfo.class.getClassLoader();
782+
}
783+
Class<?>[] ifcs = new Class<?>[] {controllerType, MethodInvocationInfo.class};
784+
return (T) Proxy.newProxyInstance(classLoader, ifcs, interceptor);
782785
}
783786

784787
else {

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -378,7 +378,15 @@ public void fromMethodCallWithObjectReturnType() {
378378
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
379379
}
380380

381-
@Test // SPR-16710
381+
@Test // SPR-16710
382+
public void fromMethodCallWithCharSequenceReturnType() {
383+
UriComponents uriComponents = fromMethodCall(
384+
on(BookingControllerWithCharSequence.class).getBooking(21L)).buildAndExpand(42);
385+
386+
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
387+
}
388+
389+
@Test // SPR-16710
382390
public void fromMethodCallWithStringReturnType() {
383391
assertThatIllegalStateException().isThrownBy(() -> {
384392
UriComponents uriComponents = fromMethodCall(
@@ -397,7 +405,6 @@ public void fromMethodNameWithStringReturnType() {
397405

398406
@Test
399407
public void fromMappingNamePlain() {
400-
401408
initWebApplicationContext(WebConfig.class);
402409

403410
this.request.setServerName("example.org");
@@ -411,7 +418,6 @@ public void fromMappingNamePlain() {
411418

412419
@Test
413420
public void fromMappingNameWithCustomBaseUrl() {
414-
415421
initWebApplicationContext(WebConfig.class);
416422

417423
UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("https://example.org:9999/base");
@@ -420,9 +426,8 @@ public void fromMappingNameWithCustomBaseUrl() {
420426
assertThat(url).isEqualTo("https://example.org:9999/base/people/123/addresses/DE");
421427
}
422428

423-
@Test // SPR-17027
429+
@Test // SPR-17027
424430
public void fromMappingNameWithEncoding() {
425-
426431
initWebApplicationContext(WebConfig.class);
427432

428433
this.request.setServerName("example.org");
@@ -436,7 +441,6 @@ public void fromMappingNameWithEncoding() {
436441

437442
@Test
438443
public void fromMappingNameWithPathWithoutLeadingSlash() {
439-
440444
initWebApplicationContext(PathWithoutLeadingSlashConfig.class);
441445

442446
this.request.setServerName("example.org");
@@ -450,7 +454,6 @@ public void fromMappingNameWithPathWithoutLeadingSlash() {
450454

451455
@Test
452456
public void fromControllerWithPrefix() {
453-
454457
initWebApplicationContext(PathPrefixWebConfig.class);
455458

456459
this.request.setScheme("https");
@@ -463,7 +466,6 @@ public void fromControllerWithPrefix() {
463466

464467
@Test
465468
public void fromMethodWithPrefix() {
466-
467469
initWebApplicationContext(PathPrefixWebConfig.class);
468470

469471
this.request.setScheme("https");
@@ -691,6 +693,17 @@ public Object getBooking(@PathVariable Long booking) {
691693
}
692694

693695

696+
@Controller
697+
@RequestMapping("/hotels/{hotel}")
698+
static class BookingControllerWithCharSequence {
699+
700+
@GetMapping("/bookings/{booking}")
701+
public CharSequence getBooking(@PathVariable Long booking) {
702+
return "url";
703+
}
704+
}
705+
706+
694707
@Controller
695708
@RequestMapping("/hotels/{hotel}")
696709
static class BookingControllerWithString {

0 commit comments

Comments
 (0)