Skip to content

Commit 491ae1e

Browse files
committed
Use MethodInvocationInfo class loader in case of JDK platform loader as well
Closes gh-30210
1 parent 66cdf43 commit 491ae1e

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,9 @@ private static <T> T initProxy(
780780

781781
else if (controllerType.isInterface()) {
782782
ClassLoader classLoader = controllerType.getClassLoader();
783-
if (classLoader == null) { // JDK interface type from bootstrap loader
783+
if (classLoader == null || classLoader.getParent() == null) {
784+
// JDK interface type from bootstrap loader or platform loader ->
785+
// use higher-level loader which can see Spring infrastructure classes
784786
classLoader = MethodInvocationInfo.class.getClassLoader();
785787
}
786788
Class<?>[] ifcs = new Class<?>[] {controllerType, MethodInvocationInfo.class};

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
24+
import java.sql.Savepoint;
2425
import java.util.Arrays;
2526
import java.util.Date;
2627
import java.util.List;
@@ -384,14 +385,6 @@ public void fromMethodCallWithObjectReturnType() {
384385
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
385386
}
386387

387-
@Test // SPR-16710
388-
public void fromMethodCallWithCharSequenceReturnType() {
389-
UriComponents uriComponents = fromMethodCall(
390-
on(BookingControllerWithCharSequence.class).getBooking(21L)).buildAndExpand(42);
391-
392-
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
393-
}
394-
395388
@Test // SPR-16710
396389
public void fromMethodCallWithStringReturnType() {
397390
assertThatIllegalStateException().isThrownBy(() -> {
@@ -409,6 +402,22 @@ public void fromMethodNameWithStringReturnType() {
409402
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
410403
}
411404

405+
@Test // gh-30210
406+
public void fromMethodCallWithCharSequenceReturnType() {
407+
UriComponents uriComponents = fromMethodCall(
408+
on(BookingControllerWithCharSequence.class).getBooking(21L)).buildAndExpand(42);
409+
410+
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
411+
}
412+
413+
@Test // gh-30210
414+
public void fromMethodCallWithJdbc30115ReturnType() {
415+
UriComponents uriComponents = fromMethodCall(
416+
on(BookingControllerWithJdbcSavepoint.class).getBooking(21L)).buildAndExpand(42);
417+
418+
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
419+
}
420+
412421
@Test
413422
public void fromMappingNamePlain() {
414423
initWebApplicationContext(WebConfig.class);
@@ -716,6 +725,17 @@ public Object getBooking(@PathVariable Long booking) {
716725
}
717726

718727

728+
@Controller
729+
@RequestMapping("/hotels/{hotel}")
730+
static class BookingControllerWithString {
731+
732+
@GetMapping("/bookings/{booking}")
733+
public String getBooking(@PathVariable Long booking) {
734+
return "url";
735+
}
736+
}
737+
738+
719739
@Controller
720740
@RequestMapping("/hotels/{hotel}")
721741
static class BookingControllerWithCharSequence {
@@ -729,11 +749,11 @@ public CharSequence getBooking(@PathVariable Long booking) {
729749

730750
@Controller
731751
@RequestMapping("/hotels/{hotel}")
732-
static class BookingControllerWithString {
752+
static class BookingControllerWithJdbcSavepoint {
733753

734754
@GetMapping("/bookings/{booking}")
735-
public String getBooking(@PathVariable Long booking) {
736-
return "url";
755+
public Savepoint getBooking(@PathVariable Long booking) {
756+
return null;
737757
}
738758
}
739759

0 commit comments

Comments
 (0)