users@jersey.java.net

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

From: Sean Landis <sean.landis_at_gmail.com>
Date: Thu, 7 Jul 2011 09:16:11 -0600

Hi Jakub,
Thanks for the reply. Here is my test case class constructor:

  public CustomerLoginWebServiceTest() throws Exception {
    super(new WebAppDescriptor.Builder("com.foo.customerlogin.jersey.webapp")
    .servletClass(JerseyServletContainer.class)
    .initParam("javax.ws.rs.Application",
CustomerLoginApplication.class.getName())
    .addFilter(ProcessingScopeFilter.class, "Processing Scope Filter")
    .addFilter(ServiceUsageTrackingFilter.class, "Service Usage
Tracking Filter")
    .build());
    service = new ServiceRequester(resource());
  }

According to the Javadoc, the WebAppDescriptor.Builder() constructor
ought find my providers but it is not. Does anything pop out as being
incorrect?

Thanks,
Sean

On Thu, Jul 7, 2011 at 3:33 AM, Jakub Podlesak
<jakub.podlesak_at_oracle.com> wrote:
> Hi Sean,
>
> You need to register the provider on the client side.
> I do not know how you initialize the Client, but one way of doing it
> could be as follows:
>
> public class CookieTest extends JerseyTest {
>
>    public CookieTest() throws Exception {
>        super(CookieInfoReaderWriter.class.getPackage().getName());
>    }
>
>  ...
> }
>
> The provider class could also be added to your client config.
>
> Should you need more details, please provide your test class
> constructors, so that i could suggest the simplest way how to register
> your provider there.
>
> ~Jakub
>
>
> On 07/06/2011 04:00 PM, Sean Landis wrote:
>>
>> 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
>>>
>
>