Repository navigation
Description
In Spring Framework 7, I noticed that calling RestClient.Builder.configureMessageConverters(...) changes how default message converters are registered.
In DefaultRestClientBuilder.initMessageConverters(), defaults are registered only when both messageConverters and convertersConfigurer are null:
private List<HttpMessageConverter<?>> initMessageConverters() {
HttpMessageConverters.ClientBuilder builder = HttpMessageConverters.forClient();
if (this.messageConverters == null && this.convertersConfigurer == null) {
builder.registerDefaults();
}
else {
if (this.messageConverters != null) {
this.messageConverters.forEach(builder::addCustomConverter);
}
if (this.convertersConfigurer != null) {
this.convertersConfigurer.accept(builder);
}
}
List<HttpMessageConverter<?>> result = new ArrayList<>();
builder.build().forEach(result::add);
return result;
}This means that as soon as configureMessageConverters(...) is called, the implicit registerDefaults() path is skipped. For example:
RestClient client = RestClient.builder()
.configureMessageConverters(builder -> {
builder.withJsonConverter(new JacksonJsonHttpMessageConverter(jsonMapper));
})
.build();My initial expectation was that this would keep the default converters and replace/configure the JSON converter. However, since registerDefaults() is not called, the configured JSON converter may not be included in the final converter list unless the user explicitly calls registerDefaults():
RestClient client = RestClient.builder()
.configureMessageConverters(builder -> {
builder.registerDefaults();
builder.withJsonConverter(new JacksonJsonHttpMessageConverter(jsonMapper));
})
.build();Is this the intended behavior?
The part that feels surprising is that the method name configureMessageConverters(...) can be interpreted as “configure the message converters used by the RestClient”, possibly starting from the default converter set. In practice, it appears to mean “fully configure the HttpMessageConverters.ClientBuilder, including deciding whether defaults should be registered”.
I understand that there are valid use cases where users may want to call disableDefaults() or provide a fully custom converter configuration. However, I am wondering why the implicit default registration is disabled merely by the presence of convertersConfigurer, rather than allowing the configurer to operate on the default converter setup by default.
More specifically, I would like to understand the design rationale behind this condition:
if (this.messageConverters == null && this.convertersConfigurer == null) {
builder.registerDefaults();
}Would it be possible or desirable for configureMessageConverters(...) to start with defaults unless the user explicitly calls disableDefaults()? Or is the intended contract that any use of configureMessageConverters(...) requires users to explicitly call registerDefaults() when they want to retain the default converters?
This is especially relevant when migrating from the previous list-based messageConverters(...) customization style, where users could modify an already initialized converter list. With the new HttpMessageConverters.ClientBuilder based API, it is easy to accidentally configure a JSON converter while ending up with no default converters at all.
If this is intended, it may be helpful to clarify in the Javadoc that configureMessageConverters(...) disables implicit default registration and that callers should invoke registerDefaults() explicitly when they want to customize the default converter set.
Activity
RockyMM commentedon May 6, 2026
I think it's solved in 7.0.7
Rebwon commentedon May 6, 2026
@bclozel
With this commit, it is now possible to customize configureMessageConverters. However, it seems that the use case of mutating an existing RestClient into a Builder and then reconfiguring already-registered HttpMessageConverters has not been fully considered.
For example, in the following case, after mutating the builder, it is possible to modify the previously registered converters via configureMessageConvertersList:
However, if no prior configuration is applied and only configureMessageConvertersList is used, the default converters are still not registered. As a result, it is not possible to modify the default configuration:
Given this, I have a couple of questions:
bclozel commentedon May 6, 2026
@Rebwon I think what you're describing here is the expected behavior, isn't it?
configureMessageConvertersListis letting you update the resulting list. At this point, the in-flight configuration information of thewith*methods are lost.Rebwon commentedon May 6, 2026
It’s clear that configureMessageConvertersList is intended to modify the final list that gets created. However, when you call mutate() on a RestClient to obtain a builder and then try to modify it using configureMessageConverters provided by RestClient.Builder, the builder does not actually hold a reference to the messageConverters themselves. Instead, it only keeps a reference to a Consumer<HttpMessageConverters.ClientBuilder> called convertersConfigurer.
As a result, if you run the second code example above, no messageConverters end up being registered at all.
Am I misunderstanding something here?
bclozel commentedon May 6, 2026
Let me experiment and get back to you. This report looks really confusing to me right now because the two parts are not strictly related.
Rebwon commentedon May 6, 2026
Sorry for the confusion.
As you mentioned, I should have either split this into two separate issues or explained the RestClient.Builder configuration more clearly.
I’ll wait for your response. Thank you.
Rebwon commentedon Jun 2, 2026
Have there been any experiments conducted regarding this issue? I’m curious about what kind of situation it is.
bclozel commentedon Oct 9, 2026
Thanks for the report and comments, and sorry for the delay.
Your two scenarios share the same root cause: today, as soon as a
configureMessageConvertersconsumer is set onRestClient.Builder, theHttpMessageConverters.ClientBuilderstarts empty and default converters are only registered if you callregisterDefaults()explicitly. Amutate()call keeps the configured consumers and not the resulting converters, so customizing converters after mutating a client also starts from an empty builder.This is inconsistent with the Spring MVC configuration and with
WebClientcodecs, which both start from the defaults. That's why we addeddisableDefaults()in 7.0.4.We're changing this in 7.1:
RestClient.Builder#configureMessageConverters(andRestTestClient.Builder) will start from the default converters, and applications can calldisableDefaults()to opt out. Because this is a behavior change, it won't be backported to 7.0.x. In the meantime, the workaround is to callregisterDefaults()first in your consumer.Spring Boot applications using the auto-configured RestClient.Builder are not affected, because Boot already registers the defaults.
Register default converters in RestClient.Builder