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>