Jersey does not actually explicitly configure an ObjectMapper instance, rather it delegates to JacksonJsonProvider
, which in turn uses a default mapper instance. You can trace through the JacksonProviderProxy code to see how it works. You can create and customize a shared mapper to be used throughout your application by defining a context resolver:
@Provider
public class ObjectMapperContextResolver implements
ContextResolver<ObjectMapper> {
private ObjectMapper mapper = null;
public ObjectMapperContextResolver() {
super();
// Illustrate configuration of the mapper instance
mapper = new ObjectMapper().configure(
SerializationConfig.Feature.WRAP_ROOT_VALUE, true).configure(
DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
The Jackson provider will retrieve its mapper instance from this resolver, and you could do the same in your code, as so:
public class MyResource {
@Context
private ContextResolver<ObjectMapper> mapperResolver;
public void someResourceMethod() {
final ObjectMapper mapper = mapperResolver.getContext(Object.class);
}
}
client
, how could you extractobjectMapper
fromclient
? – Mensural