1

I am trying to use Jersey 1.8 with Spring 4 . But I am not able to inject dependency in Rest Class using Spring.

Each time I try and invoke the autowired dependency I get NULL. Can anyone suggest why my dependency isn't being injected?

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>



    <servlet>
        <servlet-name>Rest</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>org.portal.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Rest</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>  

spring-servlet.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <context:component-scan base-package="org.portal.services"></context:component-scan>
    <context:annotation-config />

    <bean
        class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="location">
            <value>WEB-INF/dataSource.properties</value>
        </property>
    </bean>
    <import resource="hibernate.cfg.xml" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="addCountryService" class="org.portal.services.AddCountryService">
        <property name="addCountryProcessor" ref="addCountryProcessor"></property>
    </bean>

    <bean id="addCountryProcessor" class="org.portal.processors.AddCountryProcessor">
        <property name="hibernateTemplate" ref="hibernateTemplate" />
    </bean>


</beans>

Rest Coltroller

package org.portal.services;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.portal.dto.Country;
import org.portal.processors.AddCountryProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Component
@Path("/Country")
public class AddCountryService {

    @Autowired
    private AddCountryProcessor addCountryProcessor;

    @POST
    @Path("/addCountry")
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_XML)
    public String addCountry(String newCountry) {

        XStream xStream = new XStream(new DomDriver());
        xStream.alias("country", Country.class);
        xStream.alias("id", String.class);
        xStream.alias("name", String.class);
        xStream.alias("region", Integer.class);

        Country country  = (Country)xStream.fromXML(newCountry);
        System.out.println("processor is "+addCountryProcessor);

        if(country != null){
            addCountryProcessor.addCountry(country);
        }
        return newCountry;
    }

    public void setAddCountryProcessor(AddCountryProcessor addCountryProcessor) {
        this.addCountryProcessor = addCountryProcessor;
    }

    public AddCountryProcessor getAddCountryProcessor() {
        return addCountryProcessor;
    }

}

After posting request to "http://localhost:7070/HumanResourcePortal/rest/Country/addCountry", Flow is coming to "addCountry()" method but then it is printing addCountryProcessor as null

Rishikesh Darandale
  • 3,222
  • 4
  • 17
  • 35
shivam
  • 489
  • 2
  • 8
  • 22
  • Can you check logs at the time of starting your war or at deployment if you are getting error in instantiating AddCountryProcessor class or in hibernate? – Amit K Bist Oct 10 '17 at 22:57
  • No Error in console for instantiating AddCountryProcessor class – shivam Oct 11 '17 at 09:42
  • Possible duplicate of [this](https://stackoverflow.com/q/19745187/8101556) question and you should be able to solve with [this](https://stackoverflow.com/a/19751981/8101556) answer. Hope this helps. If you do not want to change the servlet then try with `@com.sun.jersey.spi.inject.Inject` instead of `Autowired`. – Rishikesh Darandale Oct 11 '17 at 11:38
  • Thanks Rishikesh, "@com.sun.jersey.spi.inject.Inject" solved the issue. – shivam Oct 11 '17 at 11:55
  • can you please elaborate me the difference between servlet class "com.sun.jersey.spi.container.servlet.ServletContainer" and "com.sun.jersey.spi.spring.container.servlet.SpringServlet" – shivam Oct 11 '17 at 11:57
  • As you can see from my web.xml, I am using servlet class for rest is "com.sun.jersey.spi.container.servlet.ServletContainer" when i changed it to "com.sun.jersey.spi.spring.container.servlet.SpringServlet" . I was getting "java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?" – shivam Oct 11 '17 at 11:59
  • Why bother with an old version of Jersey in a framework that doesn't support it anyway? – K.Nicholas Oct 11 '17 at 14:36

1 Answers1

0

There are two options available here:

  • Replace @Autowired with @com.sun.jersey.spi.inject.Inject. This @Inject annotation will inject from the configured DI framework.

  • Use com.sun.jersey.spi.spring.container.servlet.SpringServlet for deploying the your resources with spring integration. For spring integration you need to include the following dependency:

    <dependency>
      <groupId>com.sun.jersey.contribs</groupId>
      <artifactId>jersey-spring</artifactId>
      <version>1.8</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    

Spring dependencies are excluded here to use your declared one rather than the one used by jersey-spring.

Rishikesh Darandale
  • 3,222
  • 4
  • 17
  • 35