Search code examples
javajacksonrestlet

Restlet/Jackson works differently when object implements Serializable


I am sending an object with some primitive fields using Restlet with Jackson converter. Up until now it worked great. But then I needed my object to implement Serializable, because I need to store it in memcache of GAE. For some reason - when the class implements Serializable, things stop working.

Restlet sends a different string representation from before, and I can't even print that string in the server. I tried printing its byte value, char-by-char and the first numbers are:

0xfffd 0xfffd 0x0000 0x0005 0x0073 0x0072

Thanks a lot!


Solution

  • What's happening is that you are getting the result of applying DefaultConverter to the returned value rather than the JacksonConverter. DefaultConverter converts Serializable objects to ObjectRepresentation, which is a binary representation (the serialized form of the object). In the absence of a specific media type being requested, the converter logic sees a Serializable as being equally convertible to an ObjectRepresentation or a JacksonRepresentation, and due to the way converters are ordered, it chooses the former.

    There are several ways to deal with this. The simplest is to specify the desired media type explicitly (MediaType.APPLICATION_JSON) in the request.

    A more robust solution is to replace the JacksonConverter with your own custom version that scores the null Variant higher than the current value of 0.5. You'll want to do this anyway if you want to override the current JacksonRepresentation behavior of creating a new ObjectMapper for each conversion, or if you want to use Jackson 2.0. Search the restlet-discuss list for my posts about how to accomplish this. (tpeierls)

    It's quite possible that this behavior has been fixed in more recent releases, and I know that work is in progress on issues related to this, so do search the issue tracker for JacksonConverter before committing to any course of action.