Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom Jackson ObjectMapper with Spring Cloud Netflix Feign

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.

like image 378
Newbie Avatar asked Mar 07 '16 20:03

Newbie


People also ask

Is it possible to customize the underlying Jackson objectmapper?

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.

Is it possible to override the objectmapper with feign client?

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.

How do I change the default configuration in objectmapper?

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.

How to configure the mapper?

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:


2 Answers

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;
    }
}
like image 58
Newbie Avatar answered Oct 24 '22 16:10

Newbie


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>
like image 28
suiwenfeng Avatar answered Oct 24 '22 16:10

suiwenfeng