2012-07-31 92 views
8

我真的需要你的幫助。我現在正在處理這個問題數週或數月。我爲這篇長文章道歉,但我想盡可能精確地解釋問題和設置。CDI注射不在Servlets中工作

我的JAVA EE 6 web應用主要包含3個Servlet的:一個javax.faces.webapp.FacesServlet,延伸javax.servlet.http.HttpServlet一個StreamingServlet和一個爲ClientServlet延伸org.eclipse.jetty.websocket .WebSocketServlet。我想在這些Servlet中使用CDI,但它只適用於FacesServlet,更確切地說,對於JSF操作所傳送的Beans而言。 CDI注入不能與其他2個Servlet一起工作。

我的項目設置如下:Maven 3.0.4,Jetty 8.1.4(jetty-maven-plugin),MyFaces 2.1.6,Weld 1.1.8。

文件的pom.xml包含CDI的依賴關係:

... 
<dependency> 
    <groupId>javax.enterprise</groupId> 
    <artifactId>cdi-api</artifactId> 
    <version>1.1.EDR1.2</version> 
    <scope>provided</scope> 
</dependency> 
<dependency> 
    <groupId>javax.annotation</groupId> 
    <artifactId>jsr250-api</artifactId> 
    <version>1.0</version> 
</dependency> 
<dependency> 
    <groupId>org.jboss.weld.servlet</groupId> 
    <artifactId>weld-servlet</artifactId> 
    <version>${weld.version}</version> 
    <scope>runtime</scope> 
</dependency> 
... 

文件的src /主/ web應用/ WEB-INF/beans.xml中僅包含一個空豆元件用於激活CDI :

<?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> 

文件SCR /主/ web應用/ WEB-INF /碼頭-env.xml包含通過獲取bean管理的碼頭具體配置JNDI:

<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> 

<Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext"> 
    <New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource"> 
     <Arg> 
      <Ref id="webAppCtx" /> 
     </Arg> 
     <Arg>BeanManager</Arg> 
     <Arg> 
      <New class="javax.naming.Reference"> 
       <Arg>javax.enterprise.inject.spi.BeanManager</Arg> 
       <Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg> 
       <Arg /> 
      </New> 
     </Arg> 
    </New> 
</Configure> 

文件的src /主/ web應用/ WEB-INF/web.xml中包含servlet監聽器和引導CDI的BeanManager參考:

... 
<listener> 
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> 
</listener> 

<resource-env-ref> 
    <description>Object factory for the CDI Bean Manager</description> 
    <resource-env-ref-name>BeanManager</resource-env-ref-name> 
    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type> 
</resource-env-ref> 
... 

通過這些設置,我總是運行(第一行是最重要的):

2012-07-31 11:09:33,546 [main] ERROR org.jboss.weld.environment.jetty.JettyPost72Container - Unable to create JettyWeldInjector. CDI injection will not be available in Servlets, Filters or Listeners 
java.lang.IllegalArgumentException: Cannot load class for org.jboss.weld.environment.jetty.WeldDecorator 
    at org.jboss.weld.environment.servlet.util.Reflections.classForName(Reflections.java:58) 
    at org.jboss.weld.environment.jetty.JettyPost72Container.initialize(JettyPost72Container.java:66) 
    at org.jboss.weld.environment.servlet.Listener.contextInitialized(Listener.java:162) 
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:764) 
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:406) 
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:756) 
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:242) 
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1234) 
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:699) 
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467) 
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:256) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224) 
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:167) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:90) 
    at org.eclipse.jetty.server.Server.doStart(Server.java:262) 
    at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59) 
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:511) 
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:364) 
    at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:516) 
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101) 
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209) 
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) 
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) 
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84) 
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59) 
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183) 
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161) 
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320) 
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156) 
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537) 
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196) 
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141) 
    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 org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290) 
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230) 
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409) 
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352) 
Caused by: java.lang.NoClassDefFoundError: org/eclipse/jetty/servlet/ServletContextHandler$Decorator 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) 
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:415) 
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:377) 
    at org.jboss.weld.environment.servlet.util.Reflections.classForName(Reflections.java:51) 
    ... 44 more 
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.servlet.ServletContextHandler$Decorator 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:415) 
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:377) 
    ... 56 more 
運行使用 MVN碼頭的應用程序時,得到了以下錯誤

應用程序正在運行,但正如異常消息所述:CDI注入在Servlet中不可用。

幾天前,我找到了文章Starting a CDI webapp from Maven with Weld-Servlet and Jetty Plugin,特別是這個部分最後一招允許注入servlets。作者解釋說Weld需要修飾一些Jetty內部類以便在servlet中使用@Inject註解,並且我們必須告訴Jetty類加載器在哪裏找到這個類,以便讓Weld servlet對它進行修飾。

我等了這麼久,這個特殊的信息。所以我添加的文件的src /主/ web應用/ WEB-INF /碼頭-context.xml的

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> 

<Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext"> 
    <Set name="serverClasses"> 
     <Array type="java.lang.String"> 
      <Item>org.eclipse.jetty.servlet.ServletContextHandler.Decorator</Item> 
     </Array> 
    </Set> 
</Configure> 

我也更新了的pom.xml以納入文件碼頭-context.xml中(最後一行):

... 
<plugin> 
    <groupId>org.mortbay.jetty</groupId> 
    <artifactId>jetty-maven-plugin</artifactId> 
    <version>${jetty.version}</version> 
    <configuration> 
     <webApp> 
      <contextPath>/${project.build.finalName}</contextPath> 
     </webApp> 
     <jettyEnvXml>src/main/webapp/WEB-INF/jetty-env.xml</jettyEnvXml> 
     <contextXml>src/main/webapp/WEB-INF/jetty-context.xml</contextXml> 
     ... 

現在上面的錯誤消失了。我很高興爲小會兒:)

在SRC /主/資源/ log4j.properties我增加了焊接的日誌級別:

... 
log4j.logger.org.jboss.weld=DEBUG, stdout 
log4j.additivity.org.jboss.weld=false 
... 

當啓動應用程序,我得到以下焊接日誌消息:

... 
2012-07-31 12:00:08,968 [main] INFO org.jboss.weld.Bootstrap - WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously. 
2012-07-31 12:00:09,109 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000103 Enabled alternatives for Manager 
Enabled alternatives: [] [] 
Registered contexts: [interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.Dependent, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ConversationScoped, interface javax.inject.Singleton] 
Registered beans: 0 
: [] [] 
2012-07-31 12:00:09,109 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000104 Enabled decorator types for Manager 
Enabled alternatives: [] [] 
Registered contexts: [interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.Dependent, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ConversationScoped, interface javax.inject.Singleton] 
Registered beans: 0 
: [] 
2012-07-31 12:00:09,109 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000105 Enabled interceptor types for Manager 
Enabled alternatives: [] [] 
Registered contexts: [interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.Dependent, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ConversationScoped, interface javax.inject.Singleton] 
Registered beans: 0 
: [] 
2012-07-31 12:00:09,171 [main] INFO org.jboss.weld.environment.jetty.JettyPost72Container - Jetty7 detected, JSR-299 injection will be available in Listeners, Servlets and Filters. 
2012-07-31 12:00:09,265 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Implicit Bean [javax.enterprise.inject.spi.InjectionPoint] with qualifiers [@Default] 
2012-07-31 12:00:09,265 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.DependentContext 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.util.MediaType] with qualifiers [@Any @Default] 
2012-07-31 12:00:09,281 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled 
2012-07-31 12:00:09,281 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.web.LoginBean] with qualifiers [@Any @Default] 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.http.HttpRequestContext 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.http.HttpSessionContext 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.web.CustomerBean] with qualifiers [@Any @Default @Named] 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.bound.BoundConversationContext 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.ApplicationContext 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.util.LocaleBean] with qualifiers [@Any @Default @Named] 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.bound.BoundRequestContext 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.SingletonContext 
2012-07-31 12:00:09,281 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Implicit Bean [javax.enterprise.inject.Instance] with qualifiers [@Default] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.DataReader] with qualifiers [@Any @Default @Named] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.SessionBean] with qualifiers [@Any @Default @Named] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.ConnectionTable] with qualifiers [@Any @Default @Named] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.http.HttpConversationContext 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.servlet.ClientServlet] with qualifiers [@Any @Default] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.RequestContext 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-javax.enterprise.context.Conversation 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Implicit Bean [javax.enterprise.event.Event] with qualifiers [@Default] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.CustomerListBean] with qualifiers [@Any @Default] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.Customer] with qualifiers [@Any @Default] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.servlet.StreamingServlet] with qualifiers [@Any @Default] 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: org.jboss.weld.bean-flat-Built-in-org.jboss.weld.context.bound.BoundSessionContext 
2012-07-31 12:00:09,296 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000106 Bean: Managed Bean [class de.telexiom.cardiom.SessionInfo] with qualifiers [@Any @Default] 
2012-07-31 12:00:09,312 [main] DEBUG org.jboss.weld.Reflection - WELD-000601 interface javax.inject.Named is missing @Target. Weld will use this annotation, however this may make the application unportable. 
2012-07-31 12:00:09,312 [main] DEBUG org.jboss.weld.Bootstrap - WELD-000100 Weld initialized. Validating beans 
... 

我使用的Servlet 3.0 API,所述StreamingServlet和爲ClientServlet經由註釋配置。問題是,應用範圍的Bean connectionTableDataReader的沒有注入:

@WebServlet(value = "/stream", initParams = @WebInitParam(name = "maxIdleTime", value = "0")) 
public class StreamingServlet extends HttpServlet 
{ 
    @Inject 
    private ConnectionTable connectionTable; 
    ... 

@WebServlet(value = "/push", loadOnStartup = 1, initParams = @WebInitParam(name = "maxIdleTime", value = "0")) 
public class ClientServlet extends WebSocketServlet 
{ 
    @Inject 
    private ConnectionTable connectionTable; 

    @Inject 
    private DataReader dataReader; 
    ... 

調用(不)注入的屬性,我總是得到NullPointerException異常。當通過web.xml配置Servlet時,問題仍然存在。我對我做錯了什麼沒有更多的想法。結合Jetty和Maven閱讀關於CDI的所有文章,我想知道我是否是唯一有這些注入問題的人。

在我的JSF bean類UserBean.java注入工作中,我不必使用setter方法來初始化屬性connectionTable和dataReader。

在DataReader的構造函數中使用下面的輸出語句(以及在ConnectionTable類)

@ApplicationScoped 
public class DataReader implements Serializable 
{ 
    ... 
    public DataReader() 
    { 
     System.out.println("DataReader.DataReader(): " + this); 
    } 
    ... 

我想知道的2種DataReader的情況下,這顯示在標準輸出:

... 
2012-07-31 14:03:04,843 [qtp5435124-17] DEBUG org.jboss.weld.JSF - WELD-000504 Resuming conversation with id null 
31.07.2012 14:03:05 org.apache.myfaces.util.ExternalSpecifications isUnifiedELAvailable 
INFO: MyFaces Unified EL support enabled 
DataReader.DataReader(): [email protected] 
ConnectionTable.ConnectionTable(): [email protected] 

2012-07-31 14:03:19,093 [qtp5435124-16] DEBUG org.jboss.weld.JSF - WELD-000504 Resuming conversation with id null 
DataReader.DataReader(): [email protected] 

第一在基於表單的身份驗證之後打印5行,並且在使用注入的dataReader屬性首次處理JSF操作時出現最後2行。由於bean是應用程序範圍的,我真的很想知道2個構造函數的通行證。但是從應用程序的行爲來看,我認爲它沒有任何問題。

我最後的假設是莫名其妙的servlet住在分隔,獨立的上下文或容器或類似的東西。因爲它也不可能得到FacesContext中的2個其他Servlet:FacesContext.getCurrentInstance()。但我不知道這在某種程度上是否重要。

我希望有人能幫助我弄清楚這個問題。謝謝你在前進

塞巴斯蒂安

+0

這是我簡單碼頭CDI設置。 我希望這成爲一個提示。 https://gist.github.com/3613597 – user1644660 2012-09-03 21:31:18

回答

6

唔...你的配置看起來非常類似地雷。我不知道的是這個'java-web-api'maven artefact。我在一個新項目中檢查了這個項目,然後呃...它可以工作。然後我連續添加了對JSF和WebSocket的支持,並且它不再工作。最後,我想出了錯誤是什麼:我將'jetty-server'artefact添加爲我的pom.xml的依賴項,但是我沒有設置範圍。所以我使用了錯誤的服務器庫或類似的東西。現在,當使用'提供'範圍

<dependency> 
    <groupId>org.eclipse.jetty</groupId> 
    <artifactId>jetty-server</artifactId> 
    <version>${jetty.version}</version> 
    <scope>provided</scope> 
</dependency> 

一切似乎工作和依賴注入也在servlets中工作。

我有兩個關於更多的POM問題:

  1. ,你就沒有設置碼頭Maven插件的<jettyEnvXml>元素?

  2. 再次在Jetty Maven插件中,您使用<webAppConfig>元素。我使用的是<webApp>元素,因爲這是Eclipse代碼完成已知的。這些元素中的一個是否贊成另一個呢?

8

的錯誤是顯而易見的:

java.lang.NoClassDefFoundError: org/eclipse/jetty/servlet/ServletContextHandler$Decorator 

焊縫無法找到上述Jetty服務器類。它找不到這個類的原因是因爲Jetty的類加載機制。 Jetty的WebAppClassloader具有服務器類的默認列表,它不會加載:看http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading

Server Classes 
-org.eclipse.jetty.continuation.    don't hide continuation classes 
-org.eclipse.jetty.jndi.      don't hide naming classes 
-org.eclipse.jetty.plus.jaas.    don't hide jaas classes 
-org.eclipse.jetty.websocket.    don't hide websocket extension 
-org.eclipse.jetty.servlet.DefaultServlet don't hide default servlet 
org.eclipse.jetty.       hide all other jetty classes 

爲了讓碼頭裝載這個類,你需要添加「-org.eclipse.jetty.servlet。」到jetty-web.xml中的服務器類列表。

<Configure class="org.eclipse.jetty.webapp.WebAppContext"> 
    <Call name="prependServerClass"><Arg>-org.eclipse.jetty.servlet.</Arg></Call> 
</Configure> 

或撥打下面的方法,如果你使用的是嵌入式碼頭:

context.prependServerClass("-org.eclipse.jetty.servlet."); 
1

任何人嘗試應用@Roosevelt賴的建議下,我改稱爲碼頭的Web方法的名稱。 xml到<Call name="addServerClass"><Arg>-org.eclipse.jetty.servlet.</Arg></Call>,因爲我得到NoSuchMethod異常。

這是因爲在此確切錯誤消息docs here

碼頭(8.1.14.v20131031)

1

我的3個一天的經歷表明,

java.lang.NoClassDefFoundError: javax/enterprise/inject/spi/InjectionTarget

是,cdi-api.jar文件asinstall/glassfish/modules被命名爲cdi-api-1.2.jar。將名稱更改爲cdi-api.jar,錯誤消失。

規格:

  • Glassfish的4.1(下載爲zip文件),
  • jdk1.8.0_45,
  • OS X 10.7.5