0

I am using Jersey 2.7 and Spring along with Hibernate. I get a null pointer exception for a @Autowired bean declared in my applicationContext.xml

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>CWS2020POC</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <filter>
        <filter-name>CorsFilter</filter-name>
        <filter-class>com.cws.filters.CorsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CorsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>RESTService</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.cws.rest</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>RESTService</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Below is my applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.cws.rest" />
    <context:component-scan base-package="com.cws.dao" />

    <bean id="caseParticipantsDAO" class="com.cws.dao.impl.CaseParticipantsDAOImpl" />
</beans>

I have autowired the bean caseParticipantsDAO as follows

@Path("/caseParticipantsService")
@Component
public class CaseParticipantsService {

    @Autowired
    private CaseParticipantsDAO caseParticipantsDAO;

    @SuppressWarnings({})
    @Path("/getCaseParticipants/{caseId}/")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public CaseParticipants getCaseClients(@PathParam("caseId") int caseId) {

        return caseParticipantsDAO.getCaseClients(caseId);
    }
}

I get a null-pointer at this line

return caseParticipantsDAO.getCaseClients(caseId);

where caseParticipantsDAO is null.

If I manually try to load the applicatioContext.xml using the following code

ApplicationContext configFile = new ClassPathXmlApplicationContext(
         "applicationContext.xml");
         CaseParticipantsDAO caseParticipantsDAO = (CaseParticipantsDAO)
         configFile
         .getBean("caseParticipantsDAO");

Everything works as expected. But i needd to load the applicationContext.xml through web.xml and want to persist the context throughout the application so i can autowire my beans as and when needed.

pratik
  • 1
  • 4
  • 1
    How are you creating `CaseParticipantsService`? – Reimeus May 02 '14 at 19:11
  • possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – chrylis -cautiouslyoptimistic- May 02 '14 at 19:20
  • I am not specifically creating a instance of `CaseParticipantsService` class. It is a a RESTful web service that is invoked when i hit the url to call this particular service. Do I need to load the service class too separately – pratik May 03 '14 at 08:28
  • I tried extending the `CaseParticipantsService` with `SpringBeanAutowiringSupport` in the following way `public class CaseParticipantsService extends SpringBeanAutowiringSupport` and it works as expected. It doesnt give a null pointer exception anymore. But I am not sure if this is the correct approach. Please advice – pratik May 03 '14 at 08:29
  • 1
    You are using Jersey and Jersey is in control of the bean and not spring (despite your `@Component` annotation) and hence `@Autowired` isn't going to work. You have to make sure that you have proper Jersey/Spring integration (which you currently don't have). See https://jersey.java.net/documentation/latest/spring.html. – M. Deinum May 03 '14 at 13:27

1 Answers1

0

You should try to include maven dependency jersey-spring3. This should take care of jersey and spring integration.

<!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-spring -->
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>1.19.4</version>
</dependency>
Tim Davies
  • 824
  • 5
  • 17