0

i am trying to create simple Helloworld RestFul Service using Apache Wink, i am able to get output. but i am facing issue while autowiring other class to rest class with @ autowired annotation . private WebUtil webutil gives null. i am able to autowire class in diffrent project where i am using dispatchServlet.what should be done here .

This was marked as repeated question, but answer which was given their is related to spring Mvc using dispatch servlet, I already mentioned I am able to Autowired when I am using dispatch servlet in spring Mvc. But I am facing issue when using apache Wink. Please try that.

HelloWorld.java

package org.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.springframework.beans.factory.annotation.Autowired;

@Path("/helloworld")
public class HelloWorld {

    @Autowired
    private WebUtil webutil;
    @GET
    public String getMessage() {
        return "Hello World!";
    }
}

WebUtil.java

package org.test;

import org.springframework.stereotype.Component;

@Component(value="webutil")
public class WebUtil {

}

RestRegister.java

package org.test;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

     public class RestRegister extends Application {
            @Override
            public Set<Class<?>> getClasses() {
                Set<Class<?>> classes = new HashSet<>();
                classes.add(HelloWorld.class);

                return classes;
            }
    }

web.xml

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

    <display-name>RestTest</display-name>
    <description>RestTest</description>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/appContext.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>
        <servlet-name>RestServlet</servlet-name>
        <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.test.RestRegister</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>RestServlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

applicationContext

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

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

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix">
 <value>/WEB-INF/views/</value>
 </property>
 <property name="suffix">
 <value>.jsp</value>
 </property>
 </bean>
</beans>
rish1690
  • 281
  • 1
  • 3
  • 13
  • Hi I have gone through all those questions which is marked here as reference. Later I have posted this question as those answers doesn't solve the issue. I am using apache wink in which Autowired is giving null, this is different question please help – rish1690 Jan 13 '15 at 03:50
  • @chrylis Spring Autowired works fine, my question was when I don't use dispatch servlet and use apache wink rest servlet I get the issue. – rish1690 Jan 13 '15 at 04:07
  • It's because the Apache servlet isn't a Spring bean, and you'll have to either use a specific integration (I know there's one for Jersey-Spring) or manually retrieve the beans you want; `@Autowired` won't work for an unmanaged bean. – chrylis -cautiouslyoptimistic- Jan 13 '15 at 04:36

0 Answers0