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 ffced850f69..fed01453a1c 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 @@ -776,9 +776,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 99abb24debd..504707add20 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-2020 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. @@ -378,7 +378,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( @@ -397,7 +405,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromMappingNamePlain() { - initWebApplicationContext(WebConfig.class); this.request.setServerName("example.org"); @@ -411,7 +418,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromMappingNameWithCustomBaseUrl() { - initWebApplicationContext(WebConfig.class); UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("https://example.org:9999/base"); @@ -420,9 +426,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"); @@ -436,7 +441,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromMappingNameWithPathWithoutLeadingSlash() { - initWebApplicationContext(PathWithoutLeadingSlashConfig.class); this.request.setServerName("example.org"); @@ -450,7 +454,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromControllerWithPrefix() { - initWebApplicationContext(PathPrefixWebConfig.class); this.request.setScheme("https"); @@ -463,7 +466,6 @@ public class MvcUriComponentsBuilderTests { @Test public void fromMethodWithPrefix() { - initWebApplicationContext(PathPrefixWebConfig.class); this.request.setScheme("https"); @@ -691,6 +693,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 {