Is it possible to use Jackson as the serializer/marshaller for JSON data instead of JAXB when using Jersey Client API?
If so how to configure it?
JacksonJaxbJsonProvider
Common way how to use Jackson with custom configuration in Jersey client was to use JacksonJaxbJsonProvider
for example like this
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(yourObjectMapper());
Client client = ClientBuilder.newClient(new ClientConfig(provider));
Unfortunately in Jersey 2.26 they copied JacksonJaxbJsonProvider
class
from com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider
artifact (Jackson)
to org.glassfish.jersey.media:jersey-media-json-jackson
artifact (Jersey)
and changed package
from com.fasterxml.jackson.jaxrs.json
to org.glassfish.jersey.jackson.internal.jackson.jaxrs.json
.
It is still possible to use this approach it's just needed to change JacksonJaxbJsonProvider
import.
Apart from JacksonJaxbJsonProvider
being now in internal
package drawback is also
that you must know on which Jersey version your code runs which might be a problem when different dependencies require different Jersey versions.
ContextResolver
Better possibility how to configure Jackson in Jersey client is to use the same way how it is configured in Jersey server which is to create ObjectMapper
provider like this:
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class ObjectMapperProvider implements ContextResolver {
private ObjectMapper objectMapper = yourObjectMapper();
@Override
public ObjectMapper getContext(Class> objectType) {
return objectMapper;
}
}
and use it for example like this:
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(JacksonFeature.class); // usually auto-discovered
clientConfig.register(new ObjectMapperProvider());
Client client = ClientBuilder.newClient(clientConfig);
If you have both the server and the client you can reuse ObjectMapperProvider
class.
It seems that this approach works from Jersey version 2.9.