users@jersey.java.net

[Jersey] Re: Decorating response entities

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Mon, 01 Aug 2011 16:12:26 +0200

Hi,

You might be able to utilize the ResourceFilter API (see [1]).
That also gives you an opportunity to select, which resource
output would be decorated.

Another option is to "override" the default MessageBodyWriter [2] providers
by implementing a custom proxy provider, which could do the decoration
for you.
The default providers could get injected to this proxy with Providers
[3] iface
so that data serialization could be delegated to them as needed.

HTH,

~Jakub

[1]http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/ResourceFilterFactory.html
[2]http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/ext/MessageBodyWriter.html
[3]http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/ext/Providers.html

On 30.7.2011 22:32, 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 ?
>