我需要导入哪些软件包才能从playframework document获取此代码。
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( javaJdbc , cache , javaWs, filters)
libraryDependencies += "com.typesafe.akka" %% "akka-stream" % "2.4.2"
libraryDependencies += "com.typesafe.play" % "filters-helpers_2.11" % "2.4.2"
libraryDependencies += "com.google.code.gson" % "gson" % "2.8.0"
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
错误是扩展过滤器sais找不到符号过滤器
由于
build.sbt
{{1}}
答案 0 :(得分:1)
看起来它必须是mvc
包中的过滤器:
import play.mvc.Filter;
您的代码有import play.mvc.*;
,所以
错误是扩展过滤器sais找不到符号过滤器
不得出现。
<强>修正强>
您需要将setHeader
更改为withHeader
:
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import javax.inject.Inject;
import akka.stream.Materializer;
import play.mvc.*;
public class GlobalHeaders extends Filter {
@Inject
public GlobalHeaders(Materializer mat) {
super(mat);
}
@Override
public CompletionStage<Result> apply(Function<Http.RequestHeader, CompletionStage<Result>> nextFilter,Http.RequestHeader requestHeader) {
return nextFilter.apply(requestHeader).thenApply(result -> {
return result.withHeader("Access-Control-Allow-Origin", "*")
.withHeader("Allow", "*")
.withHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
.withHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent");
});
}
}
图库修复
如果您使用Play作为第三方库,则需要添加过滤器作为依赖项,例如:
的Maven:
<!-- https://mvnrepository.com/artifact/com.typesafe.play/filters-helpers_2.11 -->
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>filters-helpers_2.11</artifactId>
<version>2.5.14</version>
</dependency>
SBT:
// https://mvnrepository.com/artifact/com.typesafe.play/filters-helpers_2.11
libraryDependencies += "com.typesafe.play" % "filters-helpers_2.11" % "2.5.14"
标准安装
如果您使用标准播放安装,请不要将filters
包含在libraryDependencies
的{{1}}中。 Play已经拥有它,所以再添加一次就会发生冲突。
Play文档没有说过将过滤器添加为依赖项: https://www.playframework.com/documentation/2.5.x/ScalaHttpFilters