I got this to work by extending JacksonRepresentation and overriding the
createObjectMapper method. You can call super.createObjectMapper(), then
configure the ObjectMapper instance it returns.
To get this to work in the ConverterService, I extended
JacksonConverterService by overriding these methods:
@Override protected <T> JacksonRepresentation<T> create(MediaType
mediaType, T source)
@Override protected <T> JacksonRepresentation<T> create(Representation
source, Class<T> objectClass)
to return my extension of JacksonRepresentation. Then I removed the
JacksonConverterService from the engine and replace it with my local
extension.
I'd love to know if there's a better way.
Incidentally (and not on-topic for this list), recent Jackson releases have
included an ObjectMapper configuration mechanism called Module, which allows
you to bundle a set of related configuration information, including customer
serializers and deserializers, into a package that you can apply to your
ObjectMapper in one shot. For example, I bundled up some common
Restlet-related serialization configuration into a "Module" like this:
// SimpleModule is a Jackson-provided class that adds some
// convenience methods to the Module interface.
public class RestletJsonMapping extends SimpleModule {
public RestletJsonMapping() {
super(MODULE_NAME, MODULE_VERSION);
}
@Override public void setupModule(SetupContext context) {
// These can be called instead on the Module instance,
// you don't have to do it here.
addSerializer(MediaType.class, new MediaTypeSerializer());
addDeserializer(MediaType.class, new MediaTypeDeserializer());
addSerializer(Reference.class, new ReferenceSerializer());
addDeserializer(Reference.class, new ReferenceDeserializer());
addSerializer(Relation.class, new RelationSerializer());
addDeserializer(Relation.class, new RelationDeserializer());
addSerializer(Link.class, new LinkSerializer());
addDeserializer(Link.class, new LinkDeserializer());
SerializationConfig sc = context.getSerializationConfig();
// This is the setting to suppress serialization of nulls.
sc.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
DeserializationConfig dc = context.getDeserializationConfig();
dc.setAbstractTypeResolver(new AbstractTypeMaterializer());
super.setupModule(context);
}
// module name and version constants here
}
And I apply it like this in my overridden createObjectMapper:
protected ObjectMapper createObjectMapper() {
ObjectMapper mapper = super.createObjectMapper();
mapper.registerModule(new RestletJsonMapping());
return mapper;
}
--tim
On Thu, Feb 10, 2011 at 11:00 AM, gonzajg <[email protected]> wrote:
> Hi!
> I would like to configure JacksonConverterService to ignore null values
> when
> serializing.
> In their manual I should:
>
> objectMapper.configure(SerializationConfig.WRITE_NULL_PROPERTIES, false);
>
> I'm using Restlet 2.0.4 with automatic
> org.restlet.ext.jackson.JacksonConverter
> Problem is when I return an object it represents like:
>
> {"email":"[email protected]","password":null}
>
> I'd like not to include password without removing the field from the
> object.
> And in every similar case.
> I tried extending JacksonConverter but couldn't find any reference to the
> objectMapper or configuration.
>
> Any help is appreciated
> Thanks!
> --
> View this message in context:
> http://restlet-discuss.1400322.n2.nabble.com/Configure-ConverterService-tp6012218p6012218.html
> Sent from the Restlet Discuss mailing list archive at Nabble.com.
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2703283
>
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2703289