web services - JAVA-EE7/javax.ws.rs: Injection of EJB in REST-Resource -
web services - JAVA-EE7/javax.ws.rs: Injection of EJB in REST-Resource -
i'm trying inject stateless ejb jax-rs webservice via annotation @ejb. unfortunately ejb beingness injected null , throws nullpointerexception when beingness called, see 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 homecoming json.writeend().tostring(); } }
my webservice registered part , 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() { homecoming new hashset<class<?>>(arrays.aslist(registrationrest.class)); } }
as injected ejb implements interface fact prevents ejb beingness injected webservice/resource, understood reading article: http://netbeans.dzone.com/articles/how-to-combine-rest-and-ejb-31
as ejb name "dbdao" dao implements interface 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(); homecoming persons; } }
in above article stated not possible inject ejb implements interface.i cannot imagine or suppose overlooked something.
here 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() servlet net.bplace.mou.ui.rest.restconfiguration threw exception: org.jboss.resteasy.spi.unhandledexception: java.lang.nullpointerexception @ org.jboss.resteasy.core.synchronousdispatcher.handleapplicationexception(synchronousdispatcher.java:340) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.handleexception(synchronousdispatcher.java:214) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.handleinvokerexception(synchronousdispatcher.java:190) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.getresponse(synchronousdispatcher.java:540) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.invoke(synchronousdispatcher.java:502) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.invoke(synchronousdispatcher.java:119) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.plugins.server.servlet.servletcontainerdispatcher.service(servletcontainerdispatcher.java:208) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.plugins.server.servlet.httpservletdispatcher.service(httpservletdispatcher.java:55) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.plugins.server.servlet.httpservletdispatcher.service(httpservletdispatcher.java:50) [resteasy-jaxrs-2.3.2.final.jar:] @ javax.servlet.http.httpservlet.service(httpservlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.final.jar:1.0.0.final] @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:329) [jbossweb-7.0.13.final.jar:] @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:248) [jbossweb-7.0.13.final.jar:] @ com.ocpsoft.pretty.prettyfilter.dofilter(prettyfilter.java:145) [prettyfaces-jsf2-3.3.3.jar:] @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:280) [jbossweb-7.0.13.final.jar:] @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:248) [jbossweb-7.0.13.final.jar:] @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:275) [jbossweb-7.0.13.final.jar:] @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:161) [jbossweb-7.0.13.final.jar:] @ org.jboss.as.jpa.interceptor.webnontxemcloservalve.invoke(webnontxemcloservalve.java:50) [jboss-as-jpa-7.1.1.final.jar:7.1.1.final] @ org.jboss.as.web.security.securitycontextassociationvalve.invoke(securitycontextassociationvalve.java:153) [jboss-as-web-7.1.1.final.jar:7.1.1.final] @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:155) [jbossweb-7.0.13.final.jar:] @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:102) [jbossweb-7.0.13.final.jar:] @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:109) [jbossweb-7.0.13.final.jar:] @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:368) [jbossweb-7.0.13.final.jar:] @ org.apache.coyote.http11.http11processor.process(http11processor.java:877) [jbossweb-7.0.13.final.jar:] @ org.apache.coyote.http11.http11protocol$http11connectionhandler.process(http11protocol.java:671) [jbossweb-7.0.13.final.jar:] @ org.apache.tomcat.util.net.jioendpoint$worker.run(jioendpoint.java:930) [jbossweb-7.0.13.final.jar:] @ java.lang.thread.run(unknown source) [rt.jar:1.7.0_11] caused by: java.lang.nullpointerexception @ net.bplace.mou.ui.rest.registrationrest.getcitybycountryandzipcode(registrationrest.java:100) [classes:] @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) [rt.jar:1.7.0_11] @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) [rt.jar:1.7.0_11] @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) [rt.jar:1.7.0_11] @ java.lang.reflect.method.invoke(unknown source) [rt.jar:1.7.0_11] @ org.jboss.resteasy.core.methodinjectorimpl.invoke(methodinjectorimpl.java:155) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.resourcemethod.invokeontarget(resourcemethod.java:257) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.resourcemethod.invoke(resourcemethod.java:222) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.resourcemethod.invoke(resourcemethod.java:211) [resteasy-jaxrs-2.3.2.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.getresponse(synchronousdispatcher.java:525) [resteasy-jaxrs-2.3.2.final.jar:] ... 23 more
can help?
try this:
@applicationpath("/rest") public class restconfiguration extends application { private set<class<?>> resources = new hashset<class<?>>(); public restconfiguration () { resources.add(registrationrest.class); } @override public set<class<?>> getclasses() { homecoming resources; } }
make sure web.xml has this:
<listener> <listener-class>org.jboss.resteasy.plugins.server.servlet.resteasybootstrap</listener-class> </listener>
finally, create sure have beans.xml in web-inf folder
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> </beans>
java web-services rest java-ee ejb-3.1
Comments
Post a Comment