lundi 6 avril 2015

@Autowired null pointer exception in Jboss RestEasy Application

I'm changing my application from Jersey to RestEasy application. It's working fone with Jersey configurations in Web.xml when deployed in Wildfly 8.2.


But after changing the configurations from Jersey to RestEasy, it's still deployed but I get null pointer exception when trying to access @Autowired object's particualr method. To be more clear, the Autowired does not create a object at startup which results in this error. I was trying to clear this error for more than a day. Any help would be appreciated


Web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://ift.tt/nSRXKP" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/1eWqHMP" 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://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1jdM0fG
http://ift.tt/GArMu7 http://ift.tt/1jdLYo7"
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>


I get error if I try to access a Autowired object at any time. For example, here's my SessionFilter which tries to access 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();
}
}

}


I tried to do every solution suggested in many posts, even I've added an empty beans.xml file, it still didn't work.


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://ift.tt/pOHRsv"
}
}

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

}

Aucun commentaire:

Enregistrer un commentaire