1
votes

We are trying to migrate from websphere application server to websphere liberty profile.

As part of POC we are deploying a sample war with an EJB inside it. We injected the EJB using @EJB inside a restful web service. While calling the web service liberty is throwing NPE. Then i tried using InitialContext to lookup the EJB and its working fine.

Below are the features i enabled

  1. Jaxrs 1.1
  2. Jdbc 4.0
  3. ejbLite 3.1

We are running liberty on java 1.6.

Below is some code snippet

@EJB(beanName="StudentServiceBean")
private StudentServiceBean service;

Inside web service method

service.getStudent(id); // NPE here

Ejb class

@Stateless(name="StudentServiceBean")
public class StudentServiceBean {}

I even tried using @EJB with lookup and then tried with simple @EJB becUse the EJB is a local EJB and packed inside the war module itself

P.S. I moved the ejb to an ejb module and packed the application as an ear and tried. Still the ejb is not getting injected into the web service.

Then i created a new ejb and injected the new ejb into the existing ejb. The new ejb is injected properly. I think there is some problem with servlet container and the ejb container.

I tried it on liberty woth jee7 features also but still i am facing the same problem.

2
Can you provide some sample code snippets and a stack trace?Scott Kurz
I am extremely sorry. I am behind a proxy and it wont allow me to post from my desktop. But i am sure @EJB is failing.Krishna Chaitanya
This is almost certainly a duplicate of Inject an EJB into JAX-RS (RESTful service): JAX-RS endpoints are not enabled for JavaEE injection by default. You must either make the JAX-RS endpoint an EJB itself (that injects the other EJB), enable the WAR module for CDI, or otherwise handle the injection yourself.Brett Kail

2 Answers

0
votes

If the @EJB annotation cannot be resolved to an EJB, then you would have received a failure when the server created an instance of the class containing the @EJB annotation. Since that does not appear to be happening for you, and instead the instance is created fine, just without that field being set, then the server is not scanning your class for annotations or you have the javax.ejb.EJB annotation class packaged as part of your application.

I would recommend checking the following:

  1. Make sure the javax.ejb.EJB class is not being packaged as part of your annotation
  2. Check that the web.xml for your WAR module has a version > 2.4. WAR modules with a version of 2.4 (or earlier) will not be scanned for annotations.
  3. Check that the web.xml does not contain the setting metadata-complete="true". This setting turns off annotation scanning.
0
votes

JAX-RS 1.1 is part of EE6. The EE6 platform spec does not list JAX-RS endpoints among the EE components supporting injection (Table EE.5-1). I don't think your usage there is portable.