Announcement Announcement Module
Collapse
No announcement yet.
JAX-RS and applicationContext can't autowired Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JAX-RS and applicationContext can't autowired

    Hi,

    I created a web service with JAX-RS

    I tried to Autowired an object, but it doesn't autowired.

    Can I integrate JAX-RS service with spring?

    Here is my applicationContext.xml
    Code:
    <?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:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	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
    	   http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
    
    	<bean id="customerDAO" class="com.test.mock.IDAOImpl"/>
    
    </beans>
    Here is my web.xml
    Code:
    <?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">
    
    	<description></description>
    
    	<context-param>
    		<param-name>log4jConfigLocation</param-name>
    		<param-value>classpath:log4j.properties</param-value>
    	</context-param>
    
    	<listener>
    		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    	</listener>
    
    	<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>
    
    	<listener>
    		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    	</listener>
    
    
    	<servlet>
    		<display-name>JAX-RS REST Servlet</display-name>
    		<servlet-name>JAX-RS REST Servlet</servlet-name>
    		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>JAX-RS REST Servlet</servlet-name>
    		<url-pattern>/services/*</url-pattern>
    	</servlet-mapping>
    
    
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    </web-app>
    Here is my resource
    Code:
    @Produces("application/xml")
    @Path("getCustomer")
    public class CustomersResource
    {
    	@Autowired
    	private IDAO customerDAO;
            
            @GET
    	@Path("{id}")
    	@Produces("application/xml")
    	@Consumes("application/xml")
    	public Customer getCustomer(@QueryParam("id") String id)
    	{
    		Customer cust= customerDAO.getCustomer(id);
    		return cust;
    	}
    }
    Here is my IDAOImpl.java
    Code:
    public class IDAOImpl implements IDAO
    {
    	
    	public Customer getCustomer(String id)
    	{
    		Customer cust = new Customer();
                    cust.setId("abd123");
                    cust.setName("george");
                    return cust;
             }
    }
    For some reason customerDAO.getCustomer(id) always return null.

    Can someone tell me what did I do wrong?

    Thanks.

  • #2
    Run RestFul Services With Spring

    what i did to run restfull services with spring. i make this services sperately. its dataobject and interfaces and its implemented class. if u wish to do like that way that when u need u can deploy sperately your service. than i can help u. let me know. Thanks


    Hussain

    Comment


    • #3
      Change your servlet-class for com.sun.jersey.spi.spring.container.servlet.Spring Servlet

      like this:

      PHP Code:
      ...

      <
      servlet>
         <
      display-name>JAX-RS REST Servlet</display-name>
         <
      servlet-name>JAX-RS REST Servlet</servlet-name>
         <
      servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
         <
      load-on-startup>1</load-on-startup>
      </
      servlet>
      ... 
      Use jersey-spring

      Comment

      Working...
      X