Search code examples
javarestlet

Using Restlet - should I return Bean Representation or String Representation?


Am building my RESTful API using Restlet/Jackson and following the general policy of returning the Jackon Representation of the relevant Java Bean. For example, from the Restlet in Action book (page 108):

public Representation toJson() {
    ...  
    return new JacksonRepresentation<Mail>(mail);
}

The client app can do something like: Mail mail = mailRep.getObject();

All works fine in both a Java web client and a Java Android client. But what about clients built in other environments? (e.g. a PHP client) Can I still return the JSON representation of the Mail object as I am currently doing. Or should I be returning the explicit Json string and letting the client figure out what to do with it? Also would returning the string help in the future if I add information to the Mail method, but allow old clients to still work with the initial properties?


Solution

  • Looking at the two client side libraries I have used to access REST APIs (Restlet and google-http-client), both have ways of getting the JSON string from the object.
    In particular Restlet's Representation has getText() and Google's HttpResponse has parseAsString(). So this all seems like a complete non-issue. Editors - feel free to delete the question.