I'm running into a scenario where I need to define a one-off @FeignClient for a third party API. In this client I'd like to use a custom Jackson ObjectMapper that differs from my @Primary one. I know it is possible to override spring's feign configuration defaults however it is not clear to me how to simply override the ObjectMapper just by this specific client.
Since I launched this project, one of the most wanted feature was to add support for customizing the underlying Jackson ObjectMapper, and since version 2.1.1, this can be done either declaratively or programmatically. In this article, you are going to see how do customize the ObjectMapper when using the hibernate-types project.
I know it is possible to override spring's feign configuration defaults however it is not clear to me how to simply override the ObjectMapper just by this specific client. Show activity on this post. Per the documentation, you can provide a custom decoder for your Feign client as shown below. Show activity on this post.
ObjectMapper The simplest way to override the default configuration is to define an ObjectMapper bean and to mark it as @Primary: We should use this approach when we want to have full control over the serialization process and we don't want to allow external configuration.
The simplest way to configure the mapper is via application properties. The general structure of the configuration is as follows: As an example, if we want to disable SerializationFeature. WRITE_DATES_AS_TIMESTAMPS, we’ll add: Besides the mentioned feature categories, we can also configure property inclusion:
Per the documentation, you can provide a custom decoder for your Feign client as shown below.
Feign Client Interface:
@FeignClient(value = "foo", configuration = FooClientConfig.class)
public interface FooClient{
//Your mappings
}
Feign Client Custom Configuration:
@Configuration
public class FooClientConfig {
@Bean
public Decoder feignDecoder() {
HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());
HttpMessageConverters httpMessageConverters = new HttpMessageConverters(jacksonConverter);
ObjectFactory<HttpMessageConverters> objectFactory = () -> httpMessageConverters;
return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
}
public ObjectMapper customObjectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
//Customize as much as you want
return objectMapper;
}
}
follow @NewBie`s answer, i can give the better one...
@Bean
public Decoder feignDecoder() {
return new JacksonDecoder();
}
if you want use jackson message converter in feign client, please use JacksonDecoder, because SpringDecoder will increase average latency of feignclient call in production.
<!-- feign-jackson decoder -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<version>10.1.0</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With