users@jersey.java.net

MessageBodyReader exception in OSGi

From: Larry Touve <ltouve_at_potomacfusion.com>
Date: Mon, 12 Jul 2010 09:07:46 -0500

I'm trying to use a jersey client application in OSGi, and I'm getting a MessageBodyReader not found exception. If I run the client application as a standalone POJO, everything works fine, but when I try to run it within an OSGi bundle, I get the exception. I verified that the bundle is importing jaxb and all other packages that it needs. I'm using version 1.3 of the jersey client (and version 3.0.1 Final of Glassfish). I've also tried earlier versions of jersey-client. Has anyone seen this behavior?

The imported packages within OSGi for my bundle:

com.sun.jersey.api.client,version=0.0.0 from com.sun.jersey.glassfish.v3.osgi.jersey-gf-bundle (109)<http://localhost:8080/osgi/system/console/bundles/109>
com.sun.jersey.api.client.config,version=0.0.0 from com.sun.jersey.glassfish.v3.osgi.jersey-gf-bundle (109)<http://localhost:8080/osgi/system/console/bundles/109>
com.sun.jersey.core.util,version=0.0.0 from com.sun.jersey.glassfish.v3.osgi.jersey-gf-bundle (109)<http://localhost:8080/osgi/system/console/bundles/109>
javax.jms,version=1.1.0 from org.glassfish.javax.jms (138)<http://localhost:8080/osgi/system/console/bundles/138>
INFO: javax.naming,version=0.0.0 from org.apache.felix.framework (0)<http://localhost:8080/osgi/system/console/bundles/0> -- Overwritten by Boot Delegation
javax.ws.rs,version=1.1.1 from javax.ws.rs.jsr311-api (94)<http://localhost:8080/osgi/system/console/bundles/94>
javax.ws.rs.core,version=1.1.1 from javax.ws.rs.jsr311-api (94)<http://localhost:8080/osgi/system/console/bundles/94>
javax.ws.rs.ext,version=1.1.1 from javax.ws.rs.jsr311-api (94)<http://localhost:8080/osgi/system/console/bundles/94>
javax.xml.bind,version=2.2.1 from jaxb-api (24)<http://localhost:8080/osgi/system/console/bundles/24>
INFO: javax.xml.datatype,version=0.0.0 from org.apache.felix.framework (0)<http://localhost:8080/osgi/system/console/bundles/0> -- Overwritten by Boot Delegation
INFO: javax.xml.transform,version=0.0.0 from org.apache.felix.framework (0)<http://localhost:8080/osgi/system/console/bundles/0> -- Overwritten by Boot Delegation
INFO: javax.xml.transform.stream,version=0.0.0 from org.apache.felix.framework (0)<http://localhost:8080/osgi/system/console/bundles/0> -- Overwritten by Boot Delegation
mil.jfcom.jcms.schemas,version=0.0.0 from mil.jfcom.jcms.osgi.schemas (260)<http://localhost:8080/osgi/system/console/bundles/260>
org.osgi.framework,version=1.5.0 from org.apache.felix.framework (0)<http://localhost:8080/osgi/system/console/bundles/0>
org.osgi.service.cm,version=1.3.0 from org.apache.felix.configadmin (171)<http://localhost:8080/osgi/system/console/bundles/171>
org.osgi.util.tracker,version=1.4.0 from org.apache.felix.framework (0)<http://localhost:8080/osgi/system/console/bundles/0>


My exception:
com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java type, class java.lang.String, and MIME media type, text/plain;charset=iso-8859-1, was not found
        at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:526)
        at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:491)
        at mil.jfcom.jcms.osgi.framework.event.JCMSTimerTask.run(JCMSTimerTask.java:93)
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)

My client code. This is the pojo client. The OSGi bundle code is basically the same, but embedded in some other code within the bundle.

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import javax.ws.rs.core.MediaType;

public class JerseyClient
{
    private WebResource resource = null;

    public JerseyClient(String url)
    {
        try
        {
            ClientConfig cc = new DefaultClientConfig();
            cc.getClasses().add(String.class);
            Client client = Client.create(cc);
            resource = client.resource(url);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }


    public String getResponse()
    {
        ClientResponse response = resource.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
        String xml = response.getEntity(String.class); // This is the line that bombs in OSGi
        return xml;
    }

    public static void main(String args[])
    {
        JerseyClient c = new JerseyClient(args[0]);
        String xml = c.getResponse();
        System.out.println("Response: " + xml);
    }
}



Thanks,
 Larry