users@jersey.java.net

Re: [Jersey] Input and Output binary streams using JERSEY?

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Mon, 16 Aug 2010 22:02:55 -0700

On Mon, Aug 16, 2010 at 6:11 PM, Tauren Mills <tauren_at_groovee.com> wrote:
> Tatu,
>
> Thanks. It seemed like MessageBodyWriter complicated things. But I'm
> still interested in how it would be done that way, since I haven't
> been able to find any examples or information on how to use it.

Yes I understand that it is good to know how things should work, since
there are cases where this would be a good approach (esp. for
framework/libraries that add type support, as opposed to application
code).

> I'll try just returning byte[], but think I should probably look into
> StreamingOutput. The files I'm generating can get large and I'd rather
> stream them.
>
> Also, is it possible to have a single URL that can return either JSON
> or a binary file? For instance, my webapp requests JSON data that it
> uses to build a table structure in the DOM by passing a JSON object
> that defines all of the filtering and sorting of the table. But I'd
> like to also retrieve that same data in XLS format, filtered and
> sorted the same.

Yes, and that's where registering typed handled (MessageBodyWriter)
would make things easier.
Otherwise you will have to manually handle mapping of requested type
into appropriate converter, which can add lots of code for larger
systems.

> Does it make more sense to have two different REST urls for this, or
> can one url be used? If so, how would I do this?

If content is logically the same, just with different representation
(data format), single URL is considered cleaner and preferable to
having multiple end points.
You will just declare multiple output types (list of choices for
@Produces) and Jersey uses appropriate converter from POJO to desired
format. Or if you are doing things more manually, you will need to
access incoming accepts types (get them injected, I forget which
object you needed but it should be easy to find) and match it to
converter to use.

-+ Tatu +-