当我尝试在普通的sbt项目中使用Play WS库而不是play项目时,我必须使用play.api.Play.current,但尝试运行应用程序时会出现java.lang.RuntimeException: "There is no started application"。
当我尝试在普通的sbt项目中使用Play WS库而不是play项目时,我必须使用play.api.Play.current,但尝试运行应用程序时会出现java.lang.RuntimeException: "There is no started application"。
import play.api.libs.ws.ning.NingWSClient
val wsClient = NingWSClient()
wsClient.url("http://wwww.something.com").get()
build.sbt:
libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.4.3"
import play.api.libs.ws.ahc.AhcWSClient
implicit val actorSystem = ActorSystem()
implicit val materializer = ActorMaterializer()
wsClient.url("http://wwww.something.com").get()
//at the very end, to shutdown stuff cleanly :
wsClient.close()
actorSystem.terminate()
build.sbt:
libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.5.4"
正如评论中所指出的, 默认情况下您可能会收到来自底层async-http-client的大量冗长日志。 一种解决方法是开始配置logback.xml并将其放置在src/main/resources中。
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- The logging pattern, you might want to adapt it -->
<pattern>%d %coloredLevel %t - %logger - %message%n%xException</pattern>
</encoder>
</appender>
<!-- Here you can change the levels of specific loggers -->
<logger name="somelogger" level="INFO" />
<!-- Default logging level for every logger -->
<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>
</configuration>
如果要在play之外使用play-ws,请查看文档的"使用WSClient"部分:http://www.playframework.com/documentation/2.3.x/ScalaWS
val builder = new com.ning.http.client.AsyncHttpClientConfig.Builder()
val client = new play.api.libs.ws.ning.NingWSClient(builder.build())
val response = client.url(url).get()
build.sbt
/build.scala
文件需要什么依赖行?是否需要特殊的解析器? - Chris W.resolvers += "Typesafe Releases" at "https://repo.typesafe.com/typesafe/releases/"
和 "com.typesafe.play" %% "play-ws" % "2.3.7"
。(小提示:play-ws 依赖于 play,因此您将在项目中拥有整个 play 框架) - planetenkiller
sbt console
中完成这个操作,但是每次我都看到返回值是res23: scala.concurrent.Future[play.api.libs.ws.WSResponse] = List()
。 - Andy HaydenAwait.result()
,List()
表示当前还没有值。 - stanislav.chetvertkov