Currently using Spring Boot 3.1 with the reactive WebClient
configured like this:
@Configuration
public class MyConfig {
@Bean
WebClient webClient() {
ExchangeStrategies strategies = ExchangeStrategies.builder().codecs(clientCodecConfigurer ->
{
ObjectMapper objectMapper = createObjectMapper();
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder(objectMapper);
decoder.setMaxInMemorySize(10_000_000);
clientCodecConfigurer.customCodecs().register(decoder);
clientCodecConfigurer.customCodecs().register(new Jackson2JsonEncoder(objectMapper));
}
).build();
return WebClient.builder()
.exchangeStrategies(strategies)
.baseUrl(this.properties.baseUrl())
.build();
}
@Bean
public HttpServiceProxyFactory httpServiceProxyFactory(
WebClient webClient) {
return HttpServiceProxyFactory
.builder(WebClientAdapter.forClient(webClient))
.build();
}
@Bean
public MyRemoteServiceApi myGateway(HttpServiceProxyFactory httpServiceProxyFactory) {
return httpServiceProxyFactory.create(MyRemoteServiceApi.class);
}
private static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.registerModule(new JavaTimeModule());
return mapper;
}
}
I am using 2 customizations:
- A custom Jackson ObjectMapper because the application I am calling through the webclient uses kebab-case. The API exposed from my own application uses the normal pascalCase.
- Increase the maximum memory size the Jackson2JsonDecoder can use to read out responses.
How can I migrate this code to use the RestClient
of Spring Boot 3.2?
For the custom object mapper, I now have done this:
@Bean
public RestClient restClient() {
return RestClient.builder()
.messageConverters(httpMessageConverters -> {
httpMessageConverters.clear();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(createObjectMapper());
httpMessageConverters.add(converter);
})
.baseUrl(this.properties.baseUrl())
.build();
}
This seems to work, but I find it strange I have to first clear the list. Is this really the way it should be done?
Second question: how do I set that maxInMemorySize
? Or is that not needed with RestClient
?