users@jersey.java.net

[Jersey] Re: Question about MessageBodyReader interface

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Wed, 17 Apr 2013 21:31:07 -0700

No. Typically you do not implement MessageBodyReaders or -Writers yourself,
but they are either provided by Jersey (or other JAX-RS implementations),
or external providers. These are usually per-dataformat (JSON, XML etc)
providers.
It could be argued that type parameter for these types is useless... it
really does not buy much anything, since JAX-RS dispatchers need dynamic
handling and end up casting things anyway.

-+ Tatu +-


On Wed, Apr 17, 2013 at 9:00 PM, George Cao <matrix3456_at_gmail.com> wrote:

> Hi All,
> I am using Jersey framework 1.17 for my RESTful servces.
>
> But I got a question about the design of the MessageBodyReader interface.
>
> public interface MessageBodyReader<T> {
> boolean isReadable(Class<?> type, Type genericType,
> Annotation annotations[], MediaType mediaType);
>
> T readFrom(Class<T> type, Type genericType,
> Annotation annotations[], MediaType mediaType,
> MultivaluedMap<String, String> httpHeaders,
> InputStream entityStream) throws IOException,
> WebApplicationException;
>
> }
>
> Seems that we need write exactly one MessageBodyReader implementation per
> Class because of the type parameter T of the readFrom method argument list,
> right?
>
> The problem here is that Class<S> or Class<?> cannot assigned to Class<T>,
> even though S is a subclass of T.
>
> So I am wondering how to write a MessageBodyReader that can read all the
> classes that are subclasses of one specific class C instead write lots of
> concrete implementations?
>