当我尝试在正常的sbt项目而不是播放项目中使用Play WS库时,我必然会使用play.api.Play.current并获得 java.lang.RuntimeException:& #34;尝试运行应用程序时没有启动应用程序" 。
答案 0 :(得分:34)
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>
答案 1 :(得分:17)
要在游戏之外使用play-ws,请参阅&#34;使用WSClient&#34;文档部分: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()