users@jersey.java.net

[Jersey] Re: How to use a MessageBodyReader in JerseyTest

From: Sean Landis <sean.landis_at_gmail.com>
Date: Wed, 6 Jul 2011 08:00:21 -0600

It has been a few days and I haven't heard anything. Is this expected
behavior that the message body reader cannot be used by the client? Or
is there something I am missing to get it to work?

On Sun, Jul 3, 2011 at 2:51 PM, <sean.landis_at_gmail.com> wrote:
> I cannot get JerseyTest to find my MessageBodyReader.
> I've defined the following message body reader/writer:
>
>
> @Consumes(MediaType.TEXT_PLAIN)
> @Produces(MediaType.TEXT_PLAIN)
> @Provider
> public class CookieInfoReaderWriter implements
> MessageBodyWriter<CookieInfo>,
>  MessageBodyReader<CookieInfo> {
>
>  @Override
>  public boolean isReadable(Class<?> type, Type genericType,
> Annotation[] annotations,
>      MediaType mediaType) {
>    return type.equals(CookieInfo.class);
>  }
>
>  @Override
>  public CookieInfo readFrom(Class<CookieInfo> type, Type genericType,
> Annotation[] annotations,
>      MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
> InputStream entityStream)
>      throws IOException, WebApplicationException {
>    return new CookieInfo(entityStream);
>  }
>
>  @Override
>  public long getSize(CookieInfo t, Class<?> type, Type genericType,
> Annotation[] annotations,
>      MediaType mediaType) {
>    return -1;
>  }
>
>  @Override
>  public boolean isWriteable(Class<?> type, Type genericType,
> Annotation[] annotations,
>      MediaType mediaType) {
>    return CookieInfo.class.isAssignableFrom(type);
>  }
>
>  @Override
>  public void writeTo(CookieInfo t, Class<?> type, Type genericType,
> Annotation[] annotations,
>      MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
> OutputStream entityStream)
>      throws IOException, WebApplicationException {
>    t.write(entityStream);
>  }
> }
>
> The writer works great on the server. I'd like to be able to use the
> reader in the JerseyTest like such:
>
> CookieInfo ci =
> webResource.path("/newCookies").type(MediaType.TEXT_PLAIN).get(CookieIn
> fo.class);
>
> But I get an exception:
>
> SEVERE: A message body reader for Java class com.foo.dto.CookieInfo,
> and Java type class com.foo.dto.CookieInfo, and MIME media type
> text/plain was not found
> Jul 3, 2011 2:46:03 PM com.sun.jersey.api.client.ClientResponse
> getEntity
> SEVERE: The registered message body readers compatible with the MIME
> media type are:
> text/plain ->
>  com.sun.jersey.core.impl.provider.entity.StringProvider
>  com.sun.jersey.core.impl.provider.entity.ReaderProvider
> */* ->
>  com.sun.jersey.core.impl.provider.entity.FormProvider
>  com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
>  com.sun.jersey.core.impl.provider.entity.StringProvider
>  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
>  com.sun.jersey.core.impl.provider.entity.FileProvider
>  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
>  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
>
> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
>  com.sun.jersey.core.impl.provider.entity.ReaderProvider
>  com.sun.jersey.core.impl.provider.entity.DocumentProvider
>
> com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceRea
> der
>
> com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
>
> com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
>
> com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
>
> com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
>
> com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
>  com.sun.jersey.core.impl.provider.entity.EntityHolderReader
>
> Clearly my provider isn't found. This despite the following:
>
>  public WebServiceTest() throws Exception {
>    super(new WebAppDescriptor.Builder("com.foo.jersey.webapp")
>    .build());
>
> What I do instead...and I got the idea from the entityprovider sample
> code...is:
>
>    String stringCookies =
> createRequest("/newCookies").get(String.class);
>    CookieInfo ci = new CookieInfo(new
> ByteArrayInputStream(stringCookies.getBytes()));
>
> Which works, but is very unappealing compared to the way that doesn't
> work.
>
> What am I missing to get JerseyTest to use my message reader?
>
> Sean
>