glassfish
  1. glassfish
  2. GLASSFISH-18226

OSGi Service Injection with CDI does not work in JAX-RS

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Works as designed
    • Affects Version/s: 3.1.1
    • Fix Version/s: None
    • Labels:
      None

      Description

      The following code receives a NPE when called:

      @Path( "/hello" )
      public class ExampleResource
      {
          @Inject
          @OSGiService( dynamic = true )
          private ExampleService exampleService;
      
          @GET
          @Produces( "text/plain" )
          public String helloWorld()
          {
              return exampleService.helloWorld();
          }
      }
      

      However if the same code is used in a Servlet it works as expected:

      @WebServlet( name = "HelloServlet", urlPatterns = "/HelloServlet" )
      public class HelloServlet extends HttpServlet {
      
          @Inject
          @OSGiService( dynamic = true )
          private ExampleService exampleService;
      
          @Override
          protected void doGet( final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse )
          throws ServletException, IOException
          {
              httpServletResponse.getWriter().write( exampleService.helloWorld() );
          }
      }
      

      NPE below:

      [#|2012-01-19T17:29:37.569-0500|INFO|glassfish3.1.1|com.sun.jersey.server.impl.cdi.CDIComponentProviderFactoryInitializer|_ThreadID=101;_ThreadName=Thread-2;|CDI support is enabled|#]
      
      [#|2012-01-19T17:29:37.572-0500|INFO|glassfish3.1.1|com.sun.jersey.server.impl.application.WebApplicationImpl|_ThreadID=101;_ThreadName=Thread-2;|Initiating Jersey application, version 'Jersey: 1.9.1 09/14/2011 02:05 PM'|#]
      
      [#|2012-01-19T17:29:37.584-0500|INFO|glassfish3.1.1|com.sun.jersey.server.impl.application.DeferredResourceConfig|_ThreadID=101;_ThreadName=Thread-2;|Instantiated the Application class com.mm.osgiexample.rest.ExampleApplication|#]
      
      [#|2012-01-19T17:29:37.891-0500|SEVERE|glassfish3.1.1|com.sun.jersey.spi.container.ContainerResponse|_ThreadID=101;_ThreadName=Thread-2;|The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
      java.lang.NullPointerException
      	at com.mm.osgiexample.rest.ExampleResource.helloWorld(ExampleResource.java:28)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:601)
      	at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
      	at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
      	at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
      	at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
      	at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
      	at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
      	at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
      	at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
      	at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
      	at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
      	at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
      	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
      	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
      	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      	at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
      	at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
      	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
      	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
      	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
      	at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
      	at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
      	at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
      	at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
      	at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
      	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
      	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
      	at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
      	at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
      	at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
      	at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
      	at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
      	at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
      	at java.lang.Thread.run(Thread.java:722)
      |#]
      

        Activity

        Hide
        aaronjwhiteside added a comment -

        Same deal with

        @Resource( mappedName = "exampleService" )
        

        That works correctly in the Servlet but not in a JAX-RS resource..

        Show
        aaronjwhiteside added a comment - Same deal with @Resource( mappedName = "exampleService" ) That works correctly in the Servlet but not in a JAX-RS resource..
        Hide
        aaronjwhiteside added a comment -

        Also tried this against the latest 3.1.2 nightly build, no difference.

        Show
        aaronjwhiteside added a comment - Also tried this against the latest 3.1.2 nightly build, no difference.
        Hide
        aaronjwhiteside added a comment -

        So I found out why this wasn't working, I didn't have the annotation @RequestScoped (javax.enterprise.context.RequestScoped) on my Resource.

        Turns out that CDI is implicit for Servlet's but not for JAX-RS Resources.

        Show
        aaronjwhiteside added a comment - So I found out why this wasn't working, I didn't have the annotation @RequestScoped (javax.enterprise.context.RequestScoped) on my Resource. Turns out that CDI is implicit for Servlet's but not for JAX-RS Resources.
        Hide
        Sanjeeb Sahoo added a comment -

        Not a bug.

        Show
        Sanjeeb Sahoo added a comment - Not a bug.

          People

          • Assignee:
            Sivakumar Thyagarajan
            Reporter:
            aaronjwhiteside
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: