The changes in #25744 break some usages of LocalHostUriTemplateHandler
. The following code fails after updating from 2.4.4 to 2.4.6
restTemplate = restTemplateBuilder.defaultMessageConverters()
.uriTemplateHandler(LocalHostUriTemplateHandler(context.environment))
.errorHandler(DefaultResponseErrorHandler())
.build()
with
java.lang.IllegalArgumentException: RootUri must not be null
at org.springframework.util.Assert.notNull(Assert.java:201)
at org.springframework.boot.web.client.RootUriTemplateHandler.<init>(RootUriTemplateHandler.java:61)
at org.springframework.boot.web.client.RootUriTemplateHandler.withHandlerWrapper(RootUriTemplateHandler.java:96)
at org.springframework.boot.actuate.metrics.web.client.MetricsClientHttpRequestInterceptor.createUriTemplateHandler(MetricsClientHttpRequestInterceptor.java:105)
at org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer.customize(MetricsRestTemplateCustomizer.java:61)
at org.springframework.boot.web.client.RestTemplateBuilder.configure(RestTemplateBuilder.java:634)
at org.springframework.boot.web.client.RestTemplateBuilder.build(RestTemplateBuilder.java:589)
The LocalHostUriTemplateHandler
extends RootUriTemplateHandler
and so gets the special handling introduced here. However, it does not have the rootUri
field set and instead override the getRootUri
method, which causes this line to pass a null rootUri
Comment From: wilkinsona
Thanks for the report, @jhorstmann.
Interestingly, the auto-configured TestRestTemplate
isn't affected by this despite it using both MetricsClientHttpRequestInterceptor
and LocalHostUriTemplateHandler
. I think that's due to ordering. The factory overwrites the URI template handler with a LocalHostUriTemplateHandler
:
https://github.com/spring-projects/spring-boot/blob/c52143727a9c35b656f2f0d5298344e876785404/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizer.java#L138-L140
This leaves MetricsClientHttpRequestInterceptor
without its CapturingUriTemplateHandler
so it won't see any URI templates. I'm not sure that this matters for TestRestTemplate
as metrics are unlikely to be important for requests made in test code but it does leave things in a bit of an odd state.