0

I have a java project with two webservices, the first works fine, the spring beans are @Autowired and work as expected. I tried to use the same beans in the jersey service and am getting a null pointer exception which is coming from the DAO. Code is below, please please help!

Web.xml

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
     <url-pattern>/sprservice/*</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>jersey-services</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
  <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
  </init-param>
  <init-param>
     <param-name>com.sun.jersey.config.property.packages</param-name>
     <param-value>com.mypackage</param-value>
  </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
<servlet-mapping>
 <servlet-name>jersey-services</servlet-name>
 <url-pattern>/services/*</url-pattern>
</servlet-mapping>

application-context.xml

<context:annotation-config />
<context:component-scan base-package="com.mypackage"/>
<mvc:annotation-driven/>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean id="mywebservice" class="com.mypackage.webservice.Webservice">
</bean>

<beans profile="default">
    <jdbc:embedded-database id="dataSource"/>        
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>
</beans>

<beans profile="prod">
    <bean class="java.net.URI" id="dbUrl">
        <constructor-arg value="****"/>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="*****"/>
        <property name="username" value="******"/>
        <property name="password" value="******"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- change this to 'verify' before running as a production app -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
            </props>
        </property>
    </bean>
</beans>

service-beans.xml

<beans>
<import resource="classpath:applicationContext.xml"/>

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<jaxrs:server id="myContainer" address="/">
    <jaxrs:serviceBeans>
        <ref bean="mywebservice"/>
    </jaxrs:serviceBeans>
</jaxrs:server>

</beans>

WebService.java

Path("ws")
public class WebService {

private static final Logger logger = LogManager.getLogger("WebService");

@Resource
private LocationDAO locationdao;

   @GET
   @Path("getLocations")
   @Produces({MediaType.APPLICATION_JSON})
   public List<Location> getLocation(){
   logger.info("VA-- GETTING LOCATION");
   if(locationdao == null){
       logger.error("VA-- DAO IS NULL");
   }
   return locationdao.listLocations();  
   }

LocationDAO.java

@Service
public class LocationDAOImpl implements LocationDAO{

private static final Logger logger = LogManager.getLogger("LocationDAOImpl");

@PersistenceContext
EntityManager em;

@Transactional
public void addLocation(Location location) {
    //Do stuff....      
}
  • What about http://stackoverflow.com/questions/6672701/jersey-and-spring-integration-bean-injections-are-null-at-runtime ? The answer there suggests using SpringServlet, which understands the Spring application context config directive. – BobG Aug 27 '13 at 02:14
  • @BobG I've tried that. When I switch to springservlet I get a 404 when I hit the URLs, I figured it was better to be routed to the servlet but get a null bean..(lesser of two evils.) Still unsure of how to fix... – truthful_ness Aug 27 '13 at 15:23
  • 1
    It don't know jersey but here there is a full tuto for a Jersey + Spring webapp that uses com.sun.jersey.spi.spring.container.servlet.SpringServlet : http://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example/ – Julien Aug 27 '13 at 21:57

1 Answers1

0

Fixed. Solution was to:

  1. Remove from web.xml

< bean id="mywebservice" class="com.mypackage.webservice.Webservice"/ >

This line caused the null pointer error because I was mixing XML with annotations. I got the idea for the solution here.

  1. Annotate Webservice with @Controller - this indicates this is in the presentation layer
  2. Annotate the LocationDao with @Repository - indicates this is in persistence layer
  3. Added another class Service and annotated with @Service - indicates this is a service layer.
  4. Be sure to annotate @Autowired over the setter for the bean...this also caused null pointer error.

This improved the overall flow of my application and separated the logic into clear layers.

Community
  • 1
  • 1