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

时间:2016-07-01 16:25:24

标签: scala playframework-2.0 code-injection

我尝试创建一个基于Play documentation的日志过滤器,但我无法弄清楚发生了什么。

我收到了thios错误消息:

  

意外异常ProvisionException:无法配置,请参阅   以下错误:

     

1)注入构造函数时出错,java.lang.ClassCastException:   [Ljava.lang.Object;无法投射到[Lplay.mvc.EssentialFilter;在   过滤器。(Filters.scala:11)同时定位过滤器   找到play.http.HttpFilters       参数0在play.api.http.JavaHttpFiltersAdapter。(HttpFilters.scala:63)
  在定位时找到play.api.http.JavaHttpFiltersAdapter   play.api.http.HttpFilters       参数3在play.api.http.JavaCompatibleHttpRequestHandler。(HttpRequestHandler.scala:200)   同时定位play.api.http.JavaCompatibleHttpRequestHandler   找到play.api.http.HttpRequestHandler       参数4在play.api.DefaultApplication。(Application.scala:221)at   play.api.DefaultApplication.class(Application.scala:221)while   在定位时找到play.api.DefaultApplication   play.api.Application

     

1错误

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

这里是我的build.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")

3 个答案:

答案 0 :(得分:3)

It looks like this is a bug in the DefaultHttpFilters class's constructor. The error is present in 2.5.4 and will be fixed in the 2.5.5 milestone which is not yet available and does not have a due date. I would like a workaround for this as well.

Url to the issue: https://github.com/playframework/playframework/pull/6238

答案 1 :(得分:3)

我不知道这是否会帮助你解决这个问题。但我确实让我的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
}

答案 2 :(得分:0)

@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()