tutorial restful mkyong jax example espaƱol create java-ee ejb glassfish-3 jax-rs inject

java-ee - restful - mkyong jax rs tutorial



JAX-RS no pudo inyectar @EJB o @Inject (1)

Intento probar un mecanismo de inyección JAX-RS + EJB / CDI mínimo y actualmente estoy en un bloqueo de carretera debido a que GlassFish no puede inyectarse por el motivo que sea.

(El uso de @Inject arrojará NPE porque GlassFish no puede inyectar el POJO).

My beans.xml contiene solo

<beans />

Aquí está mi web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app 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/web-app_3_0.xsd" version="3.0"> <display-name>This is a REST Servlet</display-name> <servlet> <servlet-name>ServletAdaptor</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ServletAdaptor</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>

Aquí está la clase de recursos JAX-RS:

import java.util.List; import javax.ejb.EJB; import javax.ejb.Stateless; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Stateless @Path("/employee") public class EmployeeResource { @EJB ApplicationScopedEmployeeServiceImpl employeeService; @Path("/all") @GET @Produces({ MediaType.APPLICATION_XML }) public List<Employee> index() { return this.employeeService.getAllEmployees(); } }

Por último, pero no menos importante, el EJB real:

import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.ejb.Stateless; @Stateless public class ApplicationScopedEmployeeServiceImpl implements EmployeeService { final List<Employee> employees = new ArrayList<Employee>(); public ApplicationScopedEmployeeServiceImpl() { final Employee employee = new Employee(); employee.setEmployeeId(new Date().getTime()); employee.setFirstName("FirstName-" + employee.getEmployeeId()); employee.setMiddleName("MiddleName-" + employee.getEmployeeId()); employee.setLastName("LastName-" + employee.getEmployeeId()); employee.setScoped("ApplicationScoped"); this.employees.add(employee); } /** * {@inheritDoc} */ public List<Employee> getAllEmployees() { return this.employees; } }

Y esta es la excepción de GlassFish

[#|2011-05-13T12:37:53.584-0700|INFO|glassfish3.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=107;_ThreadName=Thread-1;|Portable JNDI names for EJB EmployeeResource : [java:global/rest-inject-0.0.1-SNAPSHOT/EmployeeResource!com.pcis.example.java.rest.resources.EmployeeResource, java:global/rest-inject-0.0.1-SNAPSHOT/EmployeeResource]|#] [#|2011-05-13T12:37:53.610-0700|INFO|glassfish3.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=107;_ThreadName=Thread-1;|Portable JNDI names for EJB ApplicationScopedEmployeeServiceImpl : [java:global/rest-inject-0.0.1-SNAPSHOT/ApplicationScopedEmployeeServiceImpl!com.pcis.example.java.rest.resources.EmployeeService, java:global/rest-inject-0.0.1-SNAPSHOT/ApplicationScopedEmployeeServiceImpl]|#] [#|2011-05-13T12:37:53.641-0700|INFO|glassfish3.1|org.hibernate.validator.engine.resolver.DefaultTraversableResolver|_ThreadID=24;_ThreadName=Thread-1;|Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.|#] [#|2011-05-13T12:37:53.898-0700|SEVERE|glassfish3.1|org.apache.catalina.core.ContainerBase|_ThreadID=24;_ThreadName=Thread-1;|ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class org.jboss.weld.servlet.WeldListener at org.apache.catalina.core.StandardContext.start(StandardContext.java:5271) at com.sun.enterprise.web.WebModule.start(WebModule.java:500) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:755) at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1980) at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1630) at com.sun.enterprise.web.WebApplication.start(WebApplication.java:100) at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130) at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269) at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:286) at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461) at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240) at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:370) at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1067) at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1247) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235) at org.glassfish.admingui.common.util.LocalDeploymentFacility$LocalDFCommandRunner.run(LocalDeploymentFacility.java:143) at org.glassfish.deployment.client.AbstractDeploymentFacility.deploy(AbstractDeploymentFacility.java:406) at org.glassfish.admingui.common.util.DeployUtil.invokeDeploymentFacility(DeployUtil.java:100) at org.glassfish.admingui.common.util.DeployUtil.deploy(DeployUtil.java:76) at org.glassfish.admingui.common.handlers.DeploymentHandler.deploy(DeploymentHandler.java:191) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.sun.jsftemplating.layout.descriptors.handler.Handler.invoke(Handler.java:442) at com.sun.jsftemplating.layout.descriptors.LayoutElementBase.dispatchHandlers(LayoutElementBase.java:420) at com.sun.jsftemplating.layout.descriptors.LayoutElementBase.dispatchHandlers(LayoutElementBase.java:394) at com.sun.jsftemplating.layout.event.CommandActionListener.invokeCommandHandlers(CommandActionListener.java:150) at com.sun.jsftemplating.layout.event.CommandActionListener.processAction(CommandActionListener.java:98) at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) at javax.faces.component.UICommand.broadcast(UICommand.java:300) at com.sun.webui.jsf.component.WebuiCommand.broadcast(WebuiCommand.java:166) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215) at com.sun.webui.jsf.util.UploadFilter.doFilter(UploadFilter.java:223) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) 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.doChainInvoke(StandardPipeline.java:600) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:96) 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:326) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:228) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013) 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:662) Caused by: java.lang.IllegalArgumentException: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class org.jboss.weld.servlet.WeldListener at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:2688) at org.apache.catalina.core.StandardContext.addApplicationListener(StandardContext.java:1932) at com.sun.enterprise.web.TomcatDeploymentConfig.configureApplicationListener(TomcatDeploymentConfig.java:234) at com.sun.enterprise.web.TomcatDeploymentConfig.configureWebModule(TomcatDeploymentConfig.java:93) at com.sun.enterprise.web.WebModuleContextConfig.start(WebModuleContextConfig.java:274) at com.sun.enterprise.web.WebModuleContextConfig.lifecycleEvent(WebModuleContextConfig.java:172) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:149) at org.apache.catalina.core.StandardContext.start(StandardContext.java:5268) ... 73 more Caused by: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class org.jboss.weld.servlet.WeldListener at org.apache.catalina.core.StandardContext.createListener(StandardContext.java:2798) at org.apache.catalina.core.StandardContext.loadListener(StandardContext.java:4745) at com.sun.enterprise.web.WebModule.loadListener(WebModule.java:1603) at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:2685) ... 80 more Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class org.jboss.weld.servlet.WeldListener at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:317) at com.sun.enterprise.web.WebContainer.createListenerInstance(WebContainer.java:734) at com.sun.enterprise.web.WebModule.createListenerInstance(WebModule.java:1981) at org.apache.catalina.core.StandardContext.createListener(StandardContext.java:2796) ... 83 more Caused by: java.lang.NullPointerException at org.jboss.weld.bootstrap.WeldBootstrap.getManager(WeldBootstrap.java:313) at org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:241) at org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:204) at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:478) at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:428) at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:300) ... 86 more |#]

¿Alguien sabría si hay un problema con JAX-RS y GF 3.1?

El código funciona si es un Servlet :)


De hecho, Jersey no maneja adecuadamente la inyección de dependencia. Hasta ahora, parece ser informado y apuntado para lanzamientos futuros. http://java.net/jira/browse/JERSEY-517

Enfrentando el mismo problema, tuve que implementar la búsqueda de contexto para obtener acceso al recurso administrado.

Intente algo como esto en su constructor de WS:

employeeService = (ApplicationScopedEmployeeServiceImpl) new InitialContext().lookup("java:global/EJB/EmployeeService");