RESTful Service Apache Wink @Autowired nullpointer

时间:2015-01-12 17:20:19

标签: java spring rest

我正在尝试使用 Apache Wink 创建简单的Helloworld RestFul服务,我可以获得输出。 但我面临着问题,而自动装配其他类使用@ autowired注释来休息。 private WebUtil webutil给出null。我能够在不同的项目中使用dispatchServlet来自动化类。应该在这里完成。

这被标记为重复的问题,但是给出的答案与使用dispatch servlet的spring Mvc有关,我已经提到过当我在Spring Mvc中使用dispatch servlet时我能够自动连接。但是在使用apache Wink时我遇到了问题。请试试。

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>

0 个答案:

没有答案