80852

JAX-RS + JBoss 7.1.1 + RESTEasy: NullPointException using CDI

Question:

I'm developing a Java EE 6 solution and also trying to figure out the root cause of why the dependency injection is not working...NullPointerException inside my service (userDao line):

REST Service

@Path("rest") public class UserRESTService { @EJB UserDAO userDao; @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Path("{type}") public String getJqGridData(@PathParam("type") String type) { System.out.println("type: " + type); List<USUARIO> usuarios = userDao.findAll(); int pageSize = 10; int pageNumber = ((int) usuarios.size()/pageSize)+1; JqGridData<USUARIO> data = new JqGridData<USUARIO>(pageNumber, 1, usuarios.size(), usuarios); System.out.println("Grid Data: " + data.getJsonString()); return data.getJsonString(); } }

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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>MyWeb</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param> <context-param> <param-name>javax.faces.application.CONFIG_FILES</param-name> <param-value>/WEB-INF/faces-config.xml</param-value> </context-param> <context-param> <param-name>com.sun.faces.writeStateAtFormEnd</param-name> <param-value>false</param-value> </context-param> <listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap </listener-class> </listener> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>bch.com.br.service.rest.RESTEasySingleton</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>

REST Singleton

public class RESTEasySingleton extends Application { private Set<Object> singletons = new HashSet(); private Set<Class<?>> empty = new HashSet(); public RESTEasySingleton() { this.singletons.add(new UserRESTService()); } public Set<Class<?>> getClasses() { return this.empty; } public Set<Object> getSingletons() { return this.singletons; } }

DAO

@Stateless public class UserDAO extends BaseDAO<USUARIO>{ @PersistenceContext(unitName="MyJPA") private EntityManager em; public UserDAO(Class<USUARIO> entityClass) { super(entityClass); } public UserDAO() { super(USUARIO.class); } @SuppressWarnings("unchecked") public List<USUARIO> findAll() { Query q = getEntityManager().createQuery("SELECT e FROM " + USUARIO.class.getName() + " e"); List<USUARIO> list = (List<USUARIO>) q.getResultList(); return list; } @Override protected EntityManager getEntityManager() { return em; } }

Although, the injection works fine inside another managed bean...

@ManagedBean @RequestScoped @Named public class UserBean { public UserBean() { // TODO Auto-generated constructor stub } private List<USUARIO> users = new ArrayList<USUARIO>(); private String name; @Transient @EJB UserDAO userDao; ... }

Any ideas?

Answer1:

Add @RequestScoped to your rest endpoint. I would also remove the reference to the resteasy bootstrap in your web.xml, since you're deploying to an EE6 container it's not needed (the application extension is enough).

Recommend

  • Count values that satisfy a constraint and return 0 for those that don't satisfy it in SPARQL
  • Show generated SQL in toplink in eclipse
  • Select an attribute with Xpath in a XML with namespace using Powershell
  • Spring custom user details service null pointer exception
  • SessionInformation expiration doesn't lead to user log out from system
  • Reading contents of a managed bean with reflection in a JSF application
  • JConsole Main class
  • JSF2.0 + Primefaces 3.0.1 + jquery 1.6.4 + p:commandLink + IE8 throws Unexpected call to method or p
  • how to ignore xml namespaces?
  • Should a web service response include empty values?
  • Feature detection of foreignObject in SVG
  • XSD with multi occurrences unordered
  • rewrite uppercase url to lowercase url htaccess
  • Splash Screen will not display
  • JBoss External Properties Files in Classpath
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Jetty 9 HashLoginService
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • How to create a file in java without a extension
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Read a local file using javascript
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • ImageMagick, replace semi-transparent white with opaque white
  • Highlight one bar in a series in highcharts?
  • Cannot connect to cassandra from Spark
  • Pass value from viewmodel to script in zk
  • Optimizing database types to compact database (SQLite)
  • Java applet as stand-alone Windows application?
  • Cross-Platform Protobuf Serialization
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Calling of Constructors in a Java
  • How to format a variable of double type
  • bootstrap to use multiple ng-app
  • PHP: When would you need the self:: keyword?
  • How to get icons for entities from eclipse?
  • coudnt use logback because of log4j
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy