I'm trying to inject a Stateless EJB into my JAX-RS webservice via Annotation @EJB. Unfortunately the EJB being injected is null and throws a NullPointerException when it is being called, see the class "RegistrationRest":
@Path("/database")
@Stateless
public class RegistrationRest {
@EJB
private DbDao dbDao;
@Path("getInfo/{name}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getInfoByName(@PathParam("name") String name {
TreeSet <String> ts = new TreeSet<String>();
JsonGenerator json = new JsonGenerator().writeStartArray();
// Lesen Info aus Datenbank
for(Persons person : dbDao.findInfoByName(name) {
ts.add(person.getName());
}
// Schreiben der gefundenen Staedte in ein JSON
for(String name : ts) {
json.write(name);
}
// Rueckgabe der Daten als JSON
return json.writeEnd().toString();
}
}
My Webservice is registered as part and via javax.ws.rs.core.Application, see code of "RestConfiguration":
package net.service.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
// other imports
@ApplicationPath("/rest")
public class RestConfiguration extends Application {
@Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(RegistrationRest.class));
}
}
As the injected EJB implements an interface that could be the fact that prevents the EJB being injected into the webservice/resource, as I understood reading this article: http://netbeans.dzone.com/articles/how-to-combine-rest-and-ejb-31
As the EJB with the name "DbDao" is a DAO which implements an interface I would like to keep:
@Stateless(name = "DbDao")
@Local
public class DbDaoImpl implements DbDao {
private final String ENTITY_MANAGER = "mouPU";
@PersistenceContext(unitName = ENTITY_MANAGER)
protected EntityManager em;
@Override
public List<Address> findInfoByName(final String name) {
@SuppressWarnings("unchecked")
List<Person> persons = em.createNamedQuery("findInfoByName").setParameter("name", name)).getResultList();
return persons;
}
}
In the above article it is stated that is not possible to inject an EJB which implements an interface.I cannot imagine that is so or I suppose I overlooked something.
Here is the stacktrace:
16:28:14,831 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/].[net.Bplace.mou.ui.rest.RestConfiguration]] (http--0.0.0.0-80-6) Servlet.service() for servlet net.Bplace.mou.ui.rest.RestConfiguration 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 com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145) [prettyfaces-jsf2-3.3.3.jar:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [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.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
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.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_11]
Caused by: java.lang.NullPointerException
at net.Bplace.mou.ui.rest.RegistrationRest.getCityByCountryAndZipCode(RegistrationRest.java:100) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_11]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_11]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_11]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_11]
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:]
... 23 more
Can you help?
Try this:
Make sure your web.xml has this:
Finally, make sure you have beans.xml in your WEB-INF folder