users@jersey.java.net

[Jersey] Re: Decorating response entities

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Sat, 30 Jul 2011 15:45:13 -0700

On Sat, Jul 30, 2011 at 1:32 PM, <sbailliez_at_gmail.com> wrote:
>
> For all rest services responses, I would like to provide a consistent
> response format which includes some headers/metadata/information
> related to the operations. Typically include in the response itself,
> the hostname, the operation timing information, etc.. I may pass
> information in http headers as well.
>
> so in json, something like:
> {
>  "headers": { "timing": 12, "hostname": "alpha-svc",
>   timestamp: "2011-08-01T00:01:02Z",  "service": { "version": "1.2.3",
> "name": "alpha" }
>   ...},
>  "data": ...
> }
>
> So the "header" parts would need to be added for all responses, whereas
> the data is effectively the response entity.
>
> Is there any particular place that one think of would be suitable to
> decorate entity response in such a way ? I was thinking it could
> probably be done by binding the headers metadata information to the
> thread local then use a special implementation of the messagebodywriter
> to decorate the entity.
>
> Thoughts ?

The way I have done this has been to just define generic wrapper like

abstract class Request<T> {
   public Map<String,Object> headers; // or getters/setters
   public long timestamp;
   public ServiceDefs service;
   public T data;
}

and using generic type with proper payload type on method signature,
routing from there as necessary (calling method that handles generic
info).

-+ Tatu +-