users@jersey.java.net

Re: [Jersey] Error using Spring Bean in Jersey resource class - Plz Help!!

From: Jose Javier García Zornoza <josejavier.garcia.zornoza_at_gmail.com>
Date: Sat, 20 Mar 2010 01:08:20 +0100

Hello
Does the error persist if you change the resource Scope from 'singleton'
to 'request'?

Jay_K escribio':
> Hello,
> I am trying to use an existing Spring Bean (which connects to my database)
> in Jersey Resource class. But I am getting NullPointerException when trying
> to call the method of this bean in my Resource class method. Below is the
> code snapshot.
>
> @Path("/portaldetails")
> @Component
> @Scope("singleton")
> public class PortalResource {
>
> private PersonService personservice;
>
> public void setPersonservice(PersonService personservice) {
> this.personservice = personservice;
> }
>
>
> @GET
> @Produces("text/plain")
> public String getLeaveDetails(@QueryParam("portalId") String portalId) {
>
> String portalParam = null;
> String personId = null;
>
> portalParam = portalId;
>
> if(portalParam != null) {
> // personservice is a Spring bean which inturn access database to fetch
> personId from portalId
>
> personId = personservice.getPersonId(portalId);
> }
>
> return personId;
> }
>
> My Web.xml has below config
>
> <servlet>
> <description>Jersey Servlet</description>
> <display-name>JerseyServlet</display-name>
> <servlet-name>JerseyServlet</servlet-name>
> <!--
> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
> -->
>
> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
> <init-param>
> <param-name>com.sun.jersey.config.property.packages</param-name>
> <param-value>org.unjspf.eleave.service</param-value>
> </init-param>
>
> <load-on-startup>3</load-on-startup>
> </servlet>
> <servlet-mapping>
> <servlet-name>JerseyServlet</servlet-name>
> <url-pattern>/resources/*</url-pattern>
> </servlet-mapping>
>
> The Spring ApplicationContext XML file has below bean definition for
> injecting spring bean into Jersey resource
> <bean id="portalservice" class="org.unjspf.eleave.service.PortalResource">
> <property name="personservice"><ref bean="personservice" /></property>
> </bean>
>
>
> After starting my Websphere server below is the console log.
>
> [3/19/10 11:42:42:086 EDT] 0000000b PackagesResou I Scanning for root
> resource and provider classes in the packages:
> org.unjspf.eleave.service
> [3/19/10 11:42:42:149 EDT] 0000000b ScanningResou I Root resource classes
> found:
> class org.unjspf.eleave.service.PortalResource
> [3/19/10 11:42:42:149 EDT] 0000000b ScanningResou I No provider classes
> found.
> [3/19/10 11:42:42:461 EDT] 0000000b WebApplicatio I Initiating Jersey
> application, version 'Jersey: 1.1.5 01/20/2010 03:55 PM'
> [3/19/10 11:42:43:571 EDT] 0000000b servlet I
> com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I:
> [ELeaveEARProject] [/OnTime] [JerseyServlet]: Initialization successful.
>
> On accessing the Jersey Resource class using below URL I get the following
> error.
> http://localhost:9081/OnTime/resources/portaldetails?portalId=john12201954
>
> [3/19/10 11:45:40:179 EDT] 0000001a ContainerResp E The RuntimeException
> could not be mapped to a response, re-throwing to the HTTP container
> java.lang.NullPointerException
> at
> org.unjspf.eleave.service.PortalResource.getLeaveDetails(PortalResource.java:81)
>
>