Gili wrote:
>
> Hi,
>
> I'd like to register a Jackson deserializer for a 3rd-party class I cannot
> annotate. I believe the only way to do this is by customizing the
> ObjectMapper that Jackson uses. I tried injecting my own ObjectMapper
> instance using Guice but it looks like
> com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy instantiates
> ObjectMapper directly.
>
> How am I supposed to register custom (de)serializers for 3rd-party
> classes?
>
> Thank you,
> Gili
>
Answering my own question. On the server side, all you need to do is
register a ContextResolve as a JAX-RS provider and JacksonProviderProxy will
use it:
@Provider
public class ObjectMapperProvider implements ContextResolver
{
@Override
public ObjectMapper getContext(Class<?> type)
{
final ObjectMapper result = new ObjectMapper();
SimpleModule module = new SimpleModule(getClass().getName(), new
Version(1, 0, 0, null)).
addDeserializer(PhoneNumber.class, new
PhoneNumberDeserializer()).addSerializer(
PhoneNumber.class, new PhoneNumberSerializer());
result.registerModule(module);
return result;
}
}
On the client side you need to register the provider as follows:
ClientConfig.getClasses().add(ObjectMapperProvider.class);
Gili
--
View this message in context: http://jersey.576304.n2.nabble.com/Customizing-ObjectMapper-tp6234597p6234646.html
Sent from the Jersey mailing list archive at Nabble.com.