在scalatra中获取“错误盯着处理程序”

时间:2018-04-02 16:18:56

标签: scala jetty-9 scalatra

我收到错误:在scalatra中启动处理程序。尝试使用scala和scalatra创建一个Web应用程序:https://www.bouvet.no/bouvet-deler/utbrudd/create-a-simple-web-app-with-scala-and-scalatra

以下是ScalatraBootstrap类的代码。

import com.affinio.api.MyServlet
import javax.servlet.ServletContext
import org.scalatra.LifeCycle
import org.scalatra.servlet.ScalatraListener

class ScalatraBootstrap extends LifeCycle {
  override def init(context: ServletContext) {
    context.mount (new MyServlet, "/*")


  }
}

以下项目结构:

enter image description here

[ERROR] Error starting handlers
java.lang.AbstractMethodError: com.affinio.api.MyServlet.org$scalatra$DynamicScope$_setter_$org$scalatra$DynamicScope$$dynamicRequest_$eq(Lscala/util/DynamicVariable;)V
    at org.scalatra.DynamicScope$class.$init$(DynamicScope.scala:47)
    at com.affinio.api.MyServlet.<init>(MyServlet.scala:6)
    at ScalatraBootstrap.init(ScalatraBootstrap.scala:8)
    at org.scalatra.servlet.ScalatraListener.configureCycleClass(ScalatraListener.scala:66)
    at org.scalatra.servlet.ScalatraListener.contextInitialized(ScalatraListener.scala:22)
    at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:540)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:135)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:510)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
    at org.mortbay.jetty.plugin.Jetty6PluginWebAppContext.doStart(Jetty6PluginWebAppContext.java:110)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
    at org.mortbay.jetty.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:156)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:222)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.plugin.Jetty6PluginServer.start(Jetty6PluginServer.java:132)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:371)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:307)
    at org.mortbay.jetty.plugin.AbstractJettyRunMojo.execute(AbstractJettyRunMojo.java:203)
    at org.mortbay.jetty.plugin.Jetty6RunMojo.execute(Jetty6RunMojo.java:184)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    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:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)

pom.xml依赖:

 <dependency>
        <groupId>org.scalatra</groupId>
       <artifactId>scalatra_2.11</artifactId>
       <version>2.5.4</version>
    </dependency>
    <dependency>
        <groupId>org.scalatra</groupId>
        <artifactId>scalatra-scalate_2.11</artifactId>
        <version>2.6.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.scalatra/scalatra-json -->
    <dependency>
        <groupId>org.scalatra</groupId>
        <artifactId>scalatra-json_2.11</artifactId>
        <version>2.6.2</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>9.4.8.v20171121</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty.orbit</groupId>
        <artifactId>javax.servlet</artifactId>
        <version>3.0.0.v201112011016</version>
    </dependency>

    <dependency>
        <groupId>org.json4s</groupId>
        <artifactId>json4s-jackson_2.11</artifactId>
        <version>3.5.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.json4s/json4s-core -->
    <dependency>
        <groupId>org.json4s</groupId>
        <artifactId>json4s-core_2.11</artifactId>
        <version>3.5.3</version>
    </dependency>

请建议如何解决此错误。提前谢谢。

1 个答案:

答案 0 :(得分:0)

java.lang.AbstractMethodError可能表示依赖项之间不兼容。 Jetty documentation声明Jetty 9.4实现了Servlet 3.1,因此请尝试使用以下版本的pom.xml

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-webapp</artifactId>
    <version>9.4.8.v20171121</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

请注意Scalatra documentation建议使用sbt构建Scalatra项目:

  

虽然大多数人都使用sbt来构建他们的Scalatra项目,但它是   也可以使用其他系统构建,例如Maven。一个   用户提供的Maven原型is available。对于大多数新用户,   但是,sbt将是最简单的入门方式。

使用SBT,以下命令将设置一个基本的Scalatra项目:

sbt new scalatra/scalatra.g8