1

I have build a maven project(packaging:war) for learning jax-rs and ejb integration. It has mainly following files:

Stateless Bean

package org.eb.stateless;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

@Stateless
public class StatelessBeanDemo
{
    @PostConstruct
    public void init()
    {
        System.out.println("I M POST-CONSTRUCT");
    }

    public void printData()
    {
        System.out.println("I M Stateless");
    }
}

Resource Interface

package org.webservice.rs;
import javax.ejb.Local;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Local
@Path("/home")
public interface IHelloWorldService
{
    @GET
    @Path("/eb/s1")
    Response callSL1();

    @GET
    @Path("/eb/s2")
    Response callSL2();
}

Resource

package org.webservice.rs;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.core.Response;
import org.eb.stateless.StatelessBeanDemo;

@Stateless
public class HelloWorldService implements IHelloWorldService
{
    @Inject                       // Use of "Inject" 
    private StatelessBeanDemo sl; // Always Null

    @EJB                          // Use of "EJB"
    private StatelessBeanDemo s2; // Sometime Null, Sometime Not

    public Response callSL1()
    {
        sl.printData();
        return Response.status(201).entity("DONE").build();
    }

    public Response callSL2()
    {
        s2.printData();
        return Response.status(201).entity("DONE").build();
    }
}

Web Service Application Path Class

package org.webservice.rs;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

public class RestServiceLocator extends Application
{
    public Set<Class<?>> getClasses()
    {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(HelloWorldService.class);
        return s;
    }
}

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>J2EEDemo</display-name>

    <context-param>
      <param-name>resteasy.jndi.resources</param-name>
      <param-value>java:module/HelloWorldService</param-value>
   </context-param>

   <listener>
      <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
   </listener>

    <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>org.webservice.rs.RestServiceLocator</param-value>
         </init-param>
     </servlet>
     <servlet-mapping>
         <servlet-name>Resteasy</servlet-name>
         <url-pattern>/*</url-pattern>
     </servlet-mapping>
</web-app>

Now when I am hitting url "http://127.0.0.1:8080/J2EEDemo/home/eb/s1", it is ALWAYS throwing following null pointer exception:

13:59:08,695 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/J2EEDemo].[Resteasy]] (http-0.0.0.0-0.0.0.0-8080-2) Servlet.service() for servlet Resteasy threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
    at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:340) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:897) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:626) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2039) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_51]
Caused by: java.lang.NullPointerException
    at org.webservice.rs.HelloWorldService.callSL1(HelloWorldService.java:26) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_51]
    at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_51]
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525) [resteasy-jaxrs-2.3.2.Final.jar:]
    ...

What more awkward is when I am hitting "http://127.0.0.1:8080/J2EEDemo/home/eb/s2", It is SOMETIMES working as per my wish (printing "DONE" and executing injected bean method) and sometimes throwing same null pointer exception. I am not able to understand what is happening.

user811602
  • 1,314
  • 2
  • 17
  • 47
  • First, taking into account the context you're working on, there's no need to have a *web.xml*. Take a look at [this](http://stackoverflow.com/questions/9373081/how-to-set-up-jax-rs-application-using-annotations-only-no-web-xml) post. Regarding your `s1` bean, as it isn't a [managed bean](http://stackoverflow.com/questions/8138232/should-i-use-ejb-or-inject), you can't injecting it using `@Inject` annotation. Regarding your `s2` bean, why are you specifying the `@Local` annotation? – António Ribeiro Mar 12 '16 at 23:47
  • @aribeiro, I am following http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/RESTEasy_EJB_Integration.html for integartion. It used '@Local' annotation with web.xml. In addition, I may have to configure many things from web.xml, so using it. – user811602 Mar 13 '16 at 03:55
  • @aribeiro, even after removing '@Local', behavior remains same – user811602 Mar 13 '16 at 04:10
  • 1
    I see downvote here. Please provide comment so that I can correct myself – user811602 Mar 13 '16 at 08:15

1 Answers1

0

I have solved the problem by adding 'beans.xml' dummy file in WEB-INF (Configuring CDI Application). (Enabling CDI)

But the question is still open. Why http://127.0.0.1:8080/J2EEDemo/home/eb/s2 working occasionally, before adding beans.xml ?

user811602
  • 1,314
  • 2
  • 17
  • 47