@Autowired Jboss RestEasy应用程序中的空指针异常

时间:2015-04-06 10:48:13

标签: java spring jboss resteasy wildfly-8

我正在将我的应用程序从Jersey更改为RestEasy应用程序。当在Wildfly 8.2中部署时,它在Web.xml中与Jersey配置一起工作。

但是在将配置从Jersey更改为RestEasy之后,它仍然已部署,但在尝试访问@Autowired对象的particualr方法时,我得到空指针异常。为了更清楚,Autowired在启动时不会创建对象,从而导致此错误。我试图清除这个错误超过一天。任何帮助将不胜感激

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>Web Application</display-name>
    <distributable />
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>

    <listener>
        <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
    </listener>

    <!-- Context Configuration locations for Spring XML files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/applicationContext.xml
            classpath:/applicationContext-resources.xml
            classpath:/applicationContext-dao.xml
            classpath:/applicationContext-service.xml
            classpath*:/applicationContext.xml
            /WEB-INF/applicationContext*.xml
        </param-value>
    </context-param>

    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param>

    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/api</param-value>
    </context-param>

    <filter>
        <filter-name>SessionFilter</filter-name>
        <filter-class>com.promarvel.filter.SessionFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SessionFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>login.html</welcome-file>
    </welcome-file-list>

</web-app>

的applicationContext.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.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
    default-lazy-init="true">

    <!-- Activates scanning of @Autowired -->
    <context:annotation-config />

    <!-- Activates scanning of @Repository and @Service -->
    <context:component-scan base-package="com.myPackage" />

    <!-- Add new DAOs here -->

    <!-- Add new Managers here -->
</beans>

如果我尝试随时访问Autowired对象,则会收到错误消息。例如,这是我的SessionFilter,它试图访问UserService.java

package com.myPackage.filter;

import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.myPackage.model.SessionDetails;
import com.myPackage.service.UserService;
import com.myPackage.util.DateUtil;

@Component
public class SessionFilter implements Filter {

    @Autowired
    private UserService userService;

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {

        try {

            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            String url = request.getRequestURI();
            String sessionId = request.getParameter("sessionId");

            SessionDetails sessionDetails = new SessionDetails();
            sessionDetails.setBrowserDetails(request.getHeader("User-Agent"));
            sessionDetails.setIpAddress("192.168.1.1");
            sessionDetails.setLoginTime(new Date());
            sessionDetails.setLoginUserName("UNKNOWN USER");
            sessionDetails.setActive(false);
            sessionDetails.setLoginStatus("INVALID SESSIONID");
            sessionDetails.setLastReplicationTime(new Date());

            //System.out.println(userService);   => Null Pointer Exception
            userService.saveLoginUserDetails(sessionDetails);    => Null Pointer Exception
            chain.doFilter(req, response);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

}

我尝试在很多帖子中建议的每个解决方案,即使我添加了一个空的beans.xml文件,它仍然无效。

的build.gradle

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: 'eclipse'

// Uses JDK 8
sourceCompatibility = 1.8
targetCompatibility = 1.8

// 1. Get dependencies from Maven local repository
// 2. Get dependencies from Maven central repository
repositories {
    mavenLocal()
    mavenCentral()
    maven  {
        url "http://repo1.maven.org/maven2"
    }
}

task explodedWar(type: Copy) {
    into "$buildDir/libs/myPackage"
    with war
}

configurations {
    provided
}
  sourceSets {
    main { compileClasspath += configurations.provided }
}

//Project dependencies
dependencies {
    //JUnit testing framework
    compile 'junit:junit:4.4'

    //Spring framework core
    compile 'org.springframework:spring-web:4.1.4.RELEASE'
    compile 'org.springframework:spring-core:4.1.4.RELEASE'
    compile 'org.springframework:spring-context:4.1.4.RELEASE'
    compile 'org.springframework:spring-context-support:4.1.4.RELEASE'
    compile 'org.springframework:spring-orm:4.1.4.RELEASE'

    compile 'org.springframework.security:spring-security-core:4.0.0.RELEASE'

    //jersyclient for REST API
    //compile 'com.sun.jersey.contribs:jersey-spring:1.18.3'
    //compile 'com.sun.jersey:jersey-server:1.18.3'

    //MySQL database driver
    compile 'mysql:mysql-connector-java:5.1.34'

    //Hibernate framework 
    compile 'org.hibernate:hibernate-core:4.3.8.Final'
    compile 'commons-dbcp:commons-dbcp:1.2.2'

    //Servlet API
    compile 'javax.servlet:servlet-api:2.5'

    //Base-64 Apache commons
    compile 'commons-codec:commons-codec:1.10'

    //log4j
    compile 'log4j:log4j:1.2.17'
    compile 'org.slf4j:slf4j-simple:1.7.10'

    //XmlBeans Equity Valuation
    compile 'org.apache.xmlbeans:xmlbeans:2.6.0'

    //Poi Equity Valuation
    compile 'org.apache.poi:poi:3.10.1'

    //Poi ooxml Equity Valuation
    compile 'org.apache.poi:poi-ooxml:3.10.1'

    //Poi ooxml Schemas Equity Valuation
    compile 'org.apache.poi:poi-ooxml-schemas:3.10.1'

    //Jacob Equity Valuation
    compile 'jacob:jacob:1.18-M2'

    //Google gson
    compile 'com.google.code.gson:gson:2.3.1'

    //compile 'com.sun.jersey:jersey-json:1.18.3'

    provided 'org.jboss.resteasy:resteasy-jaxrs:3.0.11.Final'

    war.dependsOn explodedWar

} 

2 个答案:

答案 0 :(得分:0)

尝试将<tx:annotation-driven/>放入您的application.xml,并将<context:component-scan base-package="com.myPackage" />修改为<context:component-scan base-package="com.myPackage.*" />

答案 1 :(得分:0)

带有@Autowire的空指针主要是由于两个原因:

  1. 该类未标记为Spring组件。
  2. 应用程序上下文无法识别bean。
  3. 因此,请确保根据您的用例使用@Component或子注释@Controller,@ Service,@ Repository注释您的UserService类。此外,您还在applicationContext.xml中扫描包。

    <mvc:annotation-driven></mvc:annotation-driven>
        <context:component-scan base-package="your.package.with.UserService"></context:component-scan>