users@jersey.java.net

[Jersey] NPE when using Jersey client to connect to remote REST service

From: Nelson, Ryan <Ryan.Nelson_at_Pearson.com>
Date: Thu, 31 May 2012 16:30:44 -0500

Hello,

 

I've searched and debugged before coming to you with this problem, but I
can't seem to find a way around it.

 

I'm integrating the Jersey client (and Jackson) into an existing WAR to
allow the webapp to access a remote REST service. I first did a "clean
room" test run in a sample project, and then after confirming it worked,
transferred the code into the WAR. However, something is not configured
correctly, and when I run this code:

 

ClientResponse response = r.type( MediaType.APPLICATION_JSON ).put(
ClientResponse.class, json );

 

I get an NPE on the call to type():

 

java.lang.NullPointerException

            at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)

            at
com.sun.jersey.api.client.PartialRequestBuilder.type(PartialRequestBuild
er.java:92)

            at
com.sun.jersey.api.client.WebResource.type(WebResource.java:347)

 

The MediaType and method don't seem to matter--I get the same error if I
do PLAIN_TEXT and GET.

            

I traced the problem through the Jersey code, and it seems the set of
HeaderDelegateProvider classes is not getting populated in
AbstractRuntimeDelegate, so when Jersey goes to process a "Content-Type"
header, it fails. I *think* this is because the container classloader
is not noticing the provider definition file under META-INF/services in
jersey-core.jar, but I'm not sure. And I'm not sure how to configure
the web server to find it.

 

I see in some of the basic examples the use of the
com.sun.jersey.config.property.packages property in the web.xml
<init-param>, but I'm not using the Jersey ServletContainer--in fact I'm
not using a servlet at all. This is simply to consume an external REST
resource.

 

Any suggestions on what I might be doing wrong? FYI, I'm using:

 

jersey-client 1.12 (also tried with 1.13-b01)

jersey-core 1.12 (also tried with 1.13-b01)

Tomcat 7.0.16

Java 1.6.0_26

 

Thanks,

Ryan