You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by Rentius <Re...@gmail.com> on 2015/11/13 06:05:17 UTC

Injecting EJB into Rest Exception Handler

I'm trying to inject a local @Stateless EJB into a Rest exception handler but
getting the following error.

java.lang.NullPointerException.

The maven Web project is running on Apache-tomee-1.7.1-jaxrs.

The EJB:

@Stateless(name = "Test")
public class Test {

    public void sayHello() {
        System.out.println("Hello");
    }

}

The Exception handler.

@Provider
@Stateless
public class TestExceptionHandler implements ExceptionMapper<Throwable> {

    @Context
    HttpServletRequest request;
    @Inject
    Test test;

    @Override
    public Response toResponse(Throwable throwable) {

        test.sayHello();

        return Response.ok().build();
    }    

}

I followed the tutorial
http://blog.iadvise.eu/2015/06/01/jee-using-ejb-and-context-annotations-in-a-jax-rs-provider-class/

Am I missing any configuration in the tomee server or in my code?



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Rentius <Re...@gmail.com>.
Hi Romain

I will try it on 1.7.2 thank you.

I'm using apache-tomee-1.7.1-jaxrs as indicated in the first entry of the
post.

Kind regards
Rentius



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4677045.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Romain Manni-Bucau <rm...@gmail.com>.
means you use a version < 1.7.2 cause we have this feature since the 1.7.2.


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2015-11-30 15:08 GMT+01:00 Rentius <Re...@gmail.com>:

> Thanks Romain
>
> Made the changes to my exception handler provider, tested it with the bean
> manager in a Servlet and it picks up the handler as a bean.
>
> It still gives me a NullPointer for the stateless bean that is being
> injected into the handler. I have tried every possible solution but it
> fails
> to successfully inject the stateless EJB into the exception handler
> provider.
>
> I will start looking at other solutions to the problem. Thank you.
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4677023.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.
>

Re: Injecting EJB into Rest Exception Handler

Posted by Rentius <Re...@gmail.com>.
Thanks Romain

Made the changes to my exception handler provider, tested it with the bean
manager in a Servlet and it picks up the handler as a bean.

It still gives me a NullPointer for the stateless bean that is being
injected into the handler. I have tried every possible solution but it fails
to successfully inject the stateless EJB into the exception handler
provider.

I will start looking at other solutions to the problem. Thank you.



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4677023.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Romain Manni-Bucau <rm...@gmail.com>.
well you use:

@Provider
@Stateless
// @LocalBean <== missing
public class TestExceptionHandler
  implements ExceptionMapper<Throwable>
{

  @Context
  HttpServletRequest request;

  @EJB
  Test test;

  public Response toResponse(Throwable throwable)
  {
    System.out.println("Test ejb in exception handler = " +
this.test.sayHello(100));

    return Response.ok().build();
  }
}

so not a local bean.


you can test the resolution of your handler in a test servlet for
instance injecting the bean manager and using this code:




final Set<Bean<?>> beans = bm.getBeans(clazz);
final Bean<?> bean = bm.resolve(beans);
final CreationalContextImpl<?> creationalContext =
bm.createCreationalContext(bean);
final Object instance = bm.getReference(bean, clazz, creationalContext);
// worked, instance is your EJB




Romain Manni-Bucau
@rmannibucau |  Blog | Github | LinkedIn | Tomitriber


2015-11-19 22:06 GMT-08:00 Rentius <Re...@gmail.com>:
> Thanks Romain
>
> I have copied how you configured your @Provider but still getting a null
> pointer on the bean being injected.
> Did you get my project to work, the one that I shared?
>
> ScratchPad.war
> <http://tomee-openejb.979440.n4.nabble.com/file/n4676894/ScratchPad.war>
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676894.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Rentius <Re...@gmail.com>.
Thanks Romain

I have copied how you configured your @Provider but still getting a null
pointer on the bean being injected.
Did you get my project to work, the one that I shared?

ScratchPad.war
<http://tomee-openejb.979440.n4.nabble.com/file/n4676894/ScratchPad.war>  



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676894.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Here is the test
https://git-wip-us.apache.org/repos/asf?p=tomee.git;a=blob;f=server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/EJBProviderTest.java;h=91bd32364d595439cb9b2c3479e9f31c34998783;hb=a8d71ea391552e94348b5d25d90e9a58388a3ca3

We have a CDIProviderTest next to it as well
Le 17 nov. 2015 03:06, "Rentius" <Re...@gmail.com> a écrit :

> Hi Romain
>
> I have tried with my EJB as a @LocalBean still getting
> NullPointerException.
>
> *@LocalBean
> @Stateless
> public class Test*
>
> I add @Stateless on the TestExceptionHandler when that didn't work I
> changed
> it to @RequestScoped but still same error.
>
> *@Provider
> @Stateless
> public class TestExceptionHandler implements ExceptionMapper<Throwable>*
>
> and
>
> *@Provider
> @RequestScoped
> public class TestExceptionHandler implements ExceptionMapper<Throwable>*
>
> I also tried to inject the Test EJB using:
>
> *    @EJB
>     Test test;*
>
> and
>
> *    @Inject
>     Test test;*
>
> Can you maybe post your working ScratchPad project?
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676807.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.
>

Re: Injecting EJB into Rest Exception Handler

Posted by Rentius <Re...@gmail.com>.
Hi Romain

I have tried with my EJB as a @LocalBean still getting NullPointerException.

*@LocalBean
@Stateless
public class Test*

I add @Stateless on the TestExceptionHandler when that didn't work I changed
it to @RequestScoped but still same error.

*@Provider
@Stateless
public class TestExceptionHandler implements ExceptionMapper<Throwable>*

and

*@Provider
@RequestScoped
public class TestExceptionHandler implements ExceptionMapper<Throwable>*

I also tried to inject the Test EJB using:

*    @EJB
    Test test;*

and 

*    @Inject
    Test test;*

Can you maybe post your working ScratchPad project?



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676807.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Romain Manni-Bucau <rm...@gmail.com>.
works if your EJB is @LocalBean

Romain Manni-Bucau
@rmannibucau |  Blog | Github | LinkedIn | Tomitriber


2015-11-16 6:33 GMT-08:00 Rentius <Re...@gmail.com>:
> Hi Romain
>
> See below attached project. It is a very simple project, just to try and get
> the handler to inject the EJB
> It is a rest call that takes a code, if the code is 1 throw an exception
> which triggers the exception handler.
>
> ScratchPad-1.war
> <http://tomee-openejb.979440.n4.nabble.com/file/n4676797/ScratchPad-1.war>
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676797.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Rentius <Re...@gmail.com>.
Hi Romain

See below attached project. It is a very simple project, just to try and get
the handler to inject the EJB
It is a rest call that takes a code, if the code is 1 throw an exception
which triggers the exception handler.

ScratchPad-1.war
<http://tomee-openejb.979440.n4.nabble.com/file/n4676797/ScratchPad-1.war>  



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676797.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Romain Manni-Bucau <rm...@gmail.com>.
The first log uses toString method, for an ejb it doesnt look like a pojo
toString.

Do you have a sample project to share we can use to investigate?
Le 16 nov. 2015 00:11, "Rentius" <Re...@gmail.com> a écrit :

> I'm trying to following this tutorial:
>
> http://blog.iadvise.eu/2015/06/01/jee-using-ejb-and-context-annotations-in-a-jax-rs-provider-class/
>
> So I've tried @Stateless and @RequestScoped on the Exception handler.
> Do not see any errors like the one you mentioned.
>
> What I see is the following in the server log:
> *Nov 16, 2015 10:00:30 AM
> org.apache.openejb.server.cxf.rs.CxfRsHttpListener
> configureFactory
> INFO:      scratchpad.test.TestExceptionHandler@5943e7f9*
>
> When I add the @Stateless I see this as well:
> *INFO: Auto-deploying ejb TestExceptionHandler:
> EjbDeployment(deployment-id=TestExceptionHandler)
> INFO: Auto-linking resource-ref 'java:comp/env/scratchpad' in bean
> TestExceptionHandler to Resource(id=scratchpad)
> INFO: Jndi(name=TestExceptionHandlerLocal) -->
> Ejb(deployment-id=TestExceptionHandler)
> INFO:
>
> Jndi(name=global/ScratchPad/TestExceptionHandler!javax.ws.rs.ext.ExceptionMapper)
> --> Ejb(deployment-id=TestExceptionHandler)*
>
> etc..
>
> The only error is a null pointer on the ejb when I try to invoke it in
> Runtime.
>
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676789.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.
>

Re: Injecting EJB into Rest Exception Handler

Posted by Rentius <Re...@gmail.com>.
I'm trying to following this tutorial:
http://blog.iadvise.eu/2015/06/01/jee-using-ejb-and-context-annotations-in-a-jax-rs-provider-class/

So I've tried @Stateless and @RequestScoped on the Exception handler.
Do not see any errors like the one you mentioned. 

What I see is the following in the server log:
*Nov 16, 2015 10:00:30 AM org.apache.openejb.server.cxf.rs.CxfRsHttpListener
configureFactory
INFO:      scratchpad.test.TestExceptionHandler@5943e7f9*

When I add the @Stateless I see this as well:
*INFO: Auto-deploying ejb TestExceptionHandler:
EjbDeployment(deployment-id=TestExceptionHandler)
INFO: Auto-linking resource-ref 'java:comp/env/scratchpad' in bean
TestExceptionHandler to Resource(id=scratchpad)
INFO: Jndi(name=TestExceptionHandlerLocal) -->
Ejb(deployment-id=TestExceptionHandler)
INFO:
Jndi(name=global/ScratchPad/TestExceptionHandler!javax.ws.rs.ext.ExceptionMapper)
--> Ejb(deployment-id=TestExceptionHandler)*

etc..

The only error is a null pointer on the ejb when I try to invoke it in
Runtime.




--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676789.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Do you have "Can't use CDI to create provider" in the logs?

Romain Manni-Bucau
@rmannibucau |  Blog | Github | LinkedIn | Tomitriber


2015-11-15 22:29 GMT-08:00 Rentius <Re...@gmail.com>:
> Hi Romain
>
> Thank you for the reply.
>
> So what you are saying is that my implementation should work? But in my case
> it isn't working.
> In the same project I have CDI activated as I'm injecting EJB's into other
> EJB's using @Inject and it is working.
>
> How would you recommend I implement the injection in the exception handler?
> I'm trying to get hold of the stateless EJB to do a translation before it
> gets send back over the rest call.
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676787.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Rentius <Re...@gmail.com>.
Hi Romain

Thank you for the reply.

So what you are saying is that my implementation should work? But in my case
it isn't working.
In the same project I have CDI activated as I'm injecting EJB's into other
EJB's using @Inject and it is working.

How would you recommend I implement the injection in the exception handler?
I'm trying to get hold of the stateless EJB to do a translation before it
gets send back over the rest call.



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781p4676787.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Injecting EJB into Rest Exception Handler

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Using a stateless as handler is very surprising bit should work only if cdi
is activated.

If not you have a plain pojo IIRC.
Le 14 nov. 2015 02:40, "Rentius" <Re...@gmail.com> a écrit :

> I'm trying to inject a local @Stateless EJB into a Rest exception handler
> but
> getting the following error.
>
> java.lang.NullPointerException.
>
> The maven Web project is running on Apache-tomee-1.7.1-jaxrs.
>
> The EJB:
>
> @Stateless(name = "Test")
> public class Test {
>
>     public void sayHello() {
>         System.out.println("Hello");
>     }
>
> }
>
> The Exception handler.
>
> @Provider
> @Stateless
> public class TestExceptionHandler implements ExceptionMapper<Throwable> {
>
>     @Context
>     HttpServletRequest request;
>     @Inject
>     Test test;
>
>     @Override
>     public Response toResponse(Throwable throwable) {
>
>         test.sayHello();
>
>         return Response.ok().build();
>     }
>
> }
>
> I followed the tutorial
>
> http://blog.iadvise.eu/2015/06/01/jee-using-ejb-and-context-annotations-in-a-jax-rs-provider-class/
>
> Am I missing any configuration in the tomee server or in my code?
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Injecting-EJB-into-Rest-Exception-Handler-tp4676781.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.
>