javajacksonquarkusmicroprofilequarkus-rest-client

How to configure ObjectMapper for Quarkus REST Client


I know that you can implement ObjectMapperCustomizer to configure the mapper for your Quarkus REST service. However, it's not clear in the documentation for Quarkus REST Client, whether it will use that same (global?) mapper or not. And how do you handle the situation when an external service have a different JSON naming convention than your own service? I can't find a way to configure the ObjectMapper for a REST client. I assume you can probably work around this with Jackson annotations, but I'm looking for a way to do it just by configuring an ObjectMapper.

So, basically, the question is: How do I configure a separate ObjectMapper for one specific REST client?


Solution

  • I ran into the same issue. Through a lot of trial and error I found the following works:

    First, define a class that implements JAX-RS ContextResolver:

    public class ClientObjectMapper implements ContextResolver<ObjectMapper> {
        @Override
        public ObjectMapper getContext(Class<?> type) {
            ObjectMapper om = new ObjectMapper();
            om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            om.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
            return om;
        }
    }
    

    Then on your client interface simply add @RegisterProvider(ClientObjectMapper.class)

    From the testing I've done so far, this will effectively have the same effect as if you were manually constructing the HTTP client using a ClientBuilder.