注入构造函数时出错,java.lang.ClassCastException: Play 2.5.4 - Scala

Error injecting constructor, java.lang.ClassCastException: Play 2.5.4 - Scala

我正在尝试创建一个基于 Play documentation 的日志过滤器,但我不知道发生了什么。

我收到 thios 错误消息:

Unexpected exception ProvisionException: Unable to provision, see the following errors:

1) Error injecting constructor, java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lplay.mvc.EssentialFilter; at Filters.(Filters.scala:11) while locating Filters while locating play.http.HttpFilters for parameter 0 at play.api.http.JavaHttpFiltersAdapter.(HttpFilters.scala:63)
while locating play.api.http.JavaHttpFiltersAdapter while locating play.api.http.HttpFilters for parameter 3 at play.api.http.JavaCompatibleHttpRequestHandler.(HttpRequestHandler.scala:200) while locating play.api.http.JavaCompatibleHttpRequestHandler while locating play.api.http.HttpRequestHandler for parameter 4 at play.api.DefaultApplication.(Application.scala:221) at play.api.DefaultApplication.class(Application.scala:221) while locating play.api.DefaultApplication while locating play.api.Application

1 error

但是我的 LoggingFilter 扩展了 EssentialFilter(我复制了文档代码)。 我需要为 Guice 绑定一些东西吗?

这是我的 built.sbt:

    name := """abc"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  filters,
  "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test,
  "org.webjars" %% "webjars-play" % "2.5.0",
  "org.webjars" % "react" % "0.13.1"
)

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"

routesGenerator := InjectedRoutesGenerator

这是我的 plugins.sbt :

// The Typesafe repository
resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"

// Eclpise Plugin
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.4")

// web plugins

addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3")

addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")

addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")

addSbtPlugin("org.irundaia.sbt" % "sbt-sassify" % "1.4.2")

// transform JSX assets to js
addSbtPlugin("com.github.ddispaltro" % "sbt-reactjs" % "0.5.0")

看起来这是 DefaultHttpFilters class 构造函数中的错误。该错误存在于 2.5.4 中,将在 2.5.5 里程碑中修复,该里程碑尚不可用且没有截止日期。我也想要一个解决方法。

Url 问题:https://github.com/playframework/playframework/pull/6238

我不知道这是否能帮助您解决这个问题。但我确实让我的 CORSFilter 工作了。 (播放2.5.4)

import com.google.inject.Inject;
import play.http.HttpFilters;
import play.mvc.EssentialAction;
import play.mvc.EssentialFilter;
import play.filters.cors.CORSFilter;

public class MyFilters extends EssentialFilter implements HttpFilters {

    @Inject
    private CORSFilter corsFilter;


    @Override
    public EssentialAction apply(EssentialAction next) {
        return corsFilter.asJava().apply(next);
    }

    @Override
    public EssentialFilter[] filters() {
        EssentialFilter[] result = new EssentialFilter[1];
        result[0] = this;

        return result;
    }
}

也将此添加到 application.conf

play.filters.cors{
# allow all paths
  pathPrefixes = ["/"]
  # allow all origins
  allowedOrigins = null
  allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
  # allow all headers
  allowedHttpHeaders = null
}
@Singleton
public class Filters implements HttpFilters {

    private final Environment env;
    private final CORSFilter corsFilter;

    @Inject
    public Filters(Environment env, CORSFilter corsFilter) {
        this.env = env;
        this.corsFilter = corsFilter;
    }

    @Override
    public EssentialFilter[] filters() {
        return new EssentialFilter[] { corsFilter.asJava() };
    }

}

重点是:corsFilter.asJava()