如何克服Scalatra初始化问题:NoSuchMethodError:javax.servlet.ServletContext.getFilterRegistration?

时间:2018-05-23 18:35:19

标签: scala scalatra

这是我第一次使用Scalatra,我在SBT之外使用它(使用mill构建和运行)。我得到以下错误,似乎是缺少依赖。

2018.05.23 18:26:30 [main] INFO org.scalatra.servlet.ScalatraListener - The cycle class name from the config: ScalatraBootstrap
2018.05.23 18:26:30 [main] INFO org.scalatra.servlet.ScalatraListener - Initializing life cycle class: ScalatraBootstrap
2018.05.23 18:26:30 [main] ERROR org.scalatra.servlet.ScalatraListener - Failed to initialize scalatra application at 
java.lang.NoSuchMethodError: javax.servlet.ServletContext.getFilterRegistration(Ljava/lang/String;)Ljavax/servlet/FilterRegistration;
        at org.scalatra.servlet.RichServletContext.mountFilter(RichServletContext.scala:162)
        at org.scalatra.servlet.RichServletContext.mount(RichServletContext.scala:85)
        at org.scalatra.servlet.RichServletContext.mount(RichServletContext.scala:93)
        at org.scalatra.servlet.RichServletContext.mount(RichServletContext.scala:90)
        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.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:890)
        at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:558)
        at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:853)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:370)
        at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1497)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1459)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:785)
        at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:287)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
        at org.eclipse.jetty.server.Server.start(Server.java:419)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
        at org.eclipse.jetty.server.Server.doStart(Server.java:386)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
        at com.example.JettyLauncher$.main(JettyLauncher.scala:20)
        at com.example.JettyLauncher.main(JettyLauncher.scala)

以下是我正在使用的依赖项:

val jettyVersion = "9.4.10.v20180503"

def ivyDeps = Agg(
  ivy"org.scalatra::scalatra:2.6.3",
  ivy"javax.servlet:servlet-api:2.5",
  ivy"org.eclipse.jetty:jetty-server:$jettyVersion",
  ivy"org.eclipse.jetty:jetty-servlet:$jettyVersion",
  ivy"org.eclipse.jetty:jetty-webapp:$jettyVersion",
)

到目前为止,我的JettyLauncher是来自网站的严格副本,除了我将resourceBase更改为实际存在的内容(但它没有帮助):

object JettyLauncher { // this is my entry object as specified in sbt project definition
  def main(args: Array[String]) {
    val port = if(System.getenv("PORT") != null) System.getenv("PORT").toInt else 5001

    val server = new Server(port)
    val context = new WebAppContext()
    context setContextPath "/"
    context.setResourceBase("repeater")
    context.addEventListener(new ScalatraListener)
    context.addServlet(classOf[DefaultServlet], "/")

    server.setHandler(context)

    server.start
    server.join
  }
}

我的LifeCycle课程也很少:

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

更新

我改为使用ScalatraServlet代替ScalatraServlet,但遇到类似的问题:

2018.05.23 18:39:24 [main] ERROR org.scalatra.servlet.ScalatraListener - Failed to initialize scalatra application at 
java.lang.NoSuchMethodError: javax.servlet.ServletContext.getServletRegistration(Ljava/lang/String;)Ljavax/servlet/ServletRegistration;
        at org.scalatra.servlet.RichServletContext.mountServlet(RichServletContext.scala:127)
        at org.scalatra.servlet.RichServletContext.mount(RichServletContext.scala:84)
        at org.scalatra.servlet.RichServletContext.mount(RichServletContext.scala:93)
        at org.scalatra.servlet.RichServletContext.mount(RichServletContext.scala:90)
        at ScalatraBootstrap.init(ScalatraBootstrap.scala:8)

更新2

我错过了之前发布的堆栈跟踪的另一个重要部分:

2018.05.23 18:39:24 [main] WARN org.eclipse.jetty.webapp.WebAppContext - Failed startup of context o.e.j.w.WebAppContext@3d74bf60{/,file:///home/brandon/workspace/sbh/repeater,UNAVAILABLE}
java.lang.NoSuchMethodError: javax.servlet.ServletContext.getServletRegistration(Ljava/lang/String;)Ljavax/servlet/ServletRegistration;
        at org.scalatra.servlet.RichServletContext.mountServlet(RichServletContext.scala:127)
        at org.scalatra.servlet.RichServletContext.mount(RichServletContext.scala:84)
        at org.scalatra.servlet.RichServletContext.mount(RichServletContext.scala:93)

我尝试在WEB-INF/web.xml下面加上repeater,但结果相同。

0 个答案:

没有答案