0

我正在使用 Jersey 2.7 + Weld 2.2.0 + EclipseLink (JPA 2.1) 开发一个 Web 应用程序。然后我尝试使用“jersey-gf-cdi.jar”“粘合”CDI,但是当我尝试在 Tomcat(7 或 8)上部署应用程序时,出现以下错误:

    03-May-2014 18:47:26.320 SEVERE [http-nio-8084-exec-3] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/restdemoapp]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:579)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:455)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1554)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1428)
    at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:885)
    at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:343)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:615)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-000069: An interceptor must have at least one binding, but org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$WebApplicationExceptionPreservingInterceptor has none
    at org.jboss.weld.bean.InterceptorImpl.<init>(InterceptorImpl.java:76)
    at org.jboss.weld.bean.InterceptorImpl.of(InterceptorImpl.java:66)
    at org.jboss.weld.bootstrap.AbstractBeanDeployer.createInterceptor(AbstractBeanDeployer.java:283)
    at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:225)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:78)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:75)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    ... 3 more

03-May-2014 18:47:26.321 SEVERE [http-nio-8084-exec-3] org.apache.catalina.startup.HostConfig.deployDescriptor Error deploying configuration descriptor /home/oscar/.netbeans/8.0/apache-tomcat-8.0.3.0_base/conf/Catalina/localhost/restdemoapp.xml
 java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/restdemoapp]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:579)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:455)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1554)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1428)
    at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:885)
    at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:343)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:615)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

Weld + Tomcat 运行良好,Tomcat 上的 Jersey 也运行良好。

而且,当我尝试在没有“jersey-gf-cdi.jar”的情况下部署应用程序时,我得到了这个:

type Exception report

message A MultiException has 5 exceptions. They are:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: A MultiException has 5 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Logger,parent=FacebookController,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1428967997)
2. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=GenericDAO<User>,parent=FacebookController,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1401771270)
3. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Authenticator,parent=FacebookController,qualifiers={@com.restdemoapp.domain.oauth.cdi.Providers(value=FACEBOOK)}),position=-1,optional=false,self=false,unqualified=null,726788106)
4. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.restdemoapp.service.social.FacebookController errors were found
5. java.lang.IllegalStateException: Unable to perform operation: resolve on com.restdemoapp.service.social.FacebookController

    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    com.restdemoapp.web.AuthFilter.doFilter(AuthFilter.java:30)

root cause

A MultiException has 5 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Logger,parent=FacebookController,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1428967997)
2. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=GenericDAO<User>,parent=FacebookController,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1401771270)
3. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Authenticator,parent=FacebookController,qualifiers={@com.restdemoapp.domain.oauth.cdi.Providers(value=FACEBOOK)}),position=-1,optional=false,self=false,unqualified=null,726788106)
4. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.restdemoapp.service.social.FacebookController errors were found
5. java.lang.IllegalStateException: Unable to perform operation: resolve on com.restdemoapp.service.social.FacebookController

    org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:88)
    org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:270)
    org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:414)
    org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
    org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:160)
    org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
    org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:621)
    org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:606)
    org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
    org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
    org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:103)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:128)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
    org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:110)
    org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:65)
    org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
    org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:250)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:320)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    com.restdemoapp.web.AuthFilter.doFilter(AuthFilter.java:30)

root cause

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Logger,parent=FacebookController,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1428967997)
    org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
    org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:232)
    org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:255)
    org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:414)
    org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
    org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:160)
    org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
    org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:621)
    org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:606)
    org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
    org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
    org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:103)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:128)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
    org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
    org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:110)
    org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:65)
    org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
    org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:250)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:320)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    com.restdemoapp.web.AuthFilter.doFilter(AuthFilter.java:30)

我的问题是,如何使用 Weld CDI 集成 Jersey?谢谢!

4

2 回答 2

2

当注释 javax.transaction.Transactional 丢失时,它似乎正在发生。

我解决了将以下依赖项添加到我的项目中:

    <dependency>
        <groupId>org.jboss.spec.javax.transaction</groupId>
        <artifactId>jboss-transaction-api_1.2_spec</artifactId>
        <version>1.0.0.Final</version>
    </dependency>    
于 2014-05-13T15:16:42.860 回答
0

抱歉耽搁了,这里是我之前提到的 CDI 的“胶水”:

BeanManager 在 context.xml 和 web.xml 中声明,我通过 AbstractBinder 扩展上的 jndi 定位器调用它。

package com.swapstickers;

import com.swapstickers.service.TraderService;
import com.swapstickers.service.UserService;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.glassfish.hk2.utilities.binding.AbstractBinder;

/**
 *
 * @author oscar
 */
public class AppBinder extends AbstractBinder {

    private static final Logger LOGGER = Logger.getLogger(AppBinder.class.getName());

    @Override
    protected void configure() {
        // Weld CDI
        BeanManager bm = beanManagerFromJndi();
        bind(getBean(bm, TraderService.class)).to(TraderService.class);
        bind(getBean(bm, UserService.class)).to(UserService.class);
    }

    /* Methods to integrate the Jersey CDI with Weld CDI. */

    private <T> T getBean(BeanManager bm, Class<T> clazz) {
        Bean<T> bean = (Bean<T>) bm.getBeans(clazz).iterator().next();
        CreationalContext<T> ctx = bm.createCreationalContext(bean);
        return (T) bm.getReference(bean, clazz, ctx); 
    }

    private static BeanManager beanManagerFromJndi() {
        try {
            return (BeanManager)new InitialContext().lookup("java:comp/BeanManager");
        } catch (NamingException ignored) {
            return beanManagerFromTomcatLocation();
        }
    }

    private static BeanManager beanManagerFromTomcatLocation() {
        try {
            return (BeanManager)new InitialContext().lookup("java:comp/env/BeanManager");
        } catch (NamingException ex) {
            LOGGER.log(Level.SEVERE, "CDI Bean Manager JNDI Lookup failed!", ex);
            return null;
        }
    }       
}
于 2015-01-30T10:40:53.187 回答