From ce2689eead26c73603455d33a30fb1822bc93d25 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 28 Mar 2023 13:20:49 +0200 Subject: [PATCH] Use MethodInvocationInfo class loader in case of core JDK interface type Closes gh-30210 --- .../annotation/MvcUriComponentsBuilder.java | 9 +++-- .../MvcUriComponentsBuilderTests.java | 33 +++++++++++++------ 2 files changed, 29 insertions(+), 13 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 34636e1b9f..c4d2b96af6 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 @@ -779,9 +779,12 @@ public class MvcUriComponentsBuilder { } else if (controllerType.isInterface()) { - return (T) Proxy.newProxyInstance(controllerType.getClassLoader(), - new Class[] {controllerType, MethodInvocationInfo.class}, - interceptor); + ClassLoader classLoader = controllerType.getClassLoader(); + if (classLoader == null) { // JDK interface type from bootstrap loader + classLoader = MethodInvocationInfo.class.getClassLoader(); + } + Class[] ifcs = new Class[] {controllerType, MethodInvocationInfo.class}; + return (T) Proxy.newProxyInstance(classLoader, ifcs, interceptor); } else { 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 f772da13ab..04f341ee32 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -240,7 +240,7 @@ public class MvcUriComponentsBuilderTests { assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/"); } - @Test // gh-29897 + @Test // gh-29897 public void fromMethodNameInUnmappedControllerMethod() { UriComponents uriComponents = fromMethodName(UnmappedControllerMethod.class, "getMethod").build(); @@ -384,7 +384,15 @@ public class MvcUriComponentsBuilderTests { assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21"); } - @Test // SPR-16710 + @Test // SPR-16710 + public void fromMethodCallWithCharSequenceReturnType() { + UriComponents uriComponents = fromMethodCall( + on(BookingControllerWithCharSequence.class).getBooking(21L)).buildAndExpand(42); + + assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21"); + } + + @Test // SPR-16710 public void fromMethodCallWithStringReturnType() { assertThatIllegalStateException().isThrownBy(() -> { UriComponents uriComponents = fromMethodCall( @@ -403,7 +411,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromMappingNamePlain() { - initWebApplicationContext(WebConfig.class); this.request.setServerName("example.org"); @@ -417,7 +424,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromMappingNameWithCustomBaseUrl() { - initWebApplicationContext(WebConfig.class); UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("https://example.org:9999/base"); @@ -426,9 +432,8 @@ public class MvcUriComponentsBuilderTests { assertThat(url).isEqualTo("https://example.org:9999/base/people/123/addresses/DE"); } - @Test // SPR-17027 + @Test // SPR-17027 public void fromMappingNameWithEncoding() { - initWebApplicationContext(WebConfig.class); this.request.setServerName("example.org"); @@ -442,7 +447,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromMappingNameWithPathWithoutLeadingSlash() { - initWebApplicationContext(PathWithoutLeadingSlashConfig.class); this.request.setServerName("example.org"); @@ -456,7 +460,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromControllerWithPrefix() { - initWebApplicationContext(PathPrefixWebConfig.class); this.request.setScheme("https"); @@ -470,7 +473,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromMethodWithPrefix() { - initWebApplicationContext(PathPrefixWebConfig.class); this.request.setScheme("https"); @@ -714,6 +716,17 @@ public class MvcUriComponentsBuilderTests { } + @Controller + @RequestMapping("/hotels/{hotel}") + static class BookingControllerWithCharSequence { + + @GetMapping("/bookings/{booking}") + public CharSequence getBooking(@PathVariable Long booking) { + return "url"; + } + } + + @Controller @RequestMapping("/hotels/{hotel}") static class BookingControllerWithString {