scala 我如何在普通 sbt 项目中使用 play ws 库而不是 play?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24881145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:25:49  来源:igfitidea点击:

How do i use play ws library in normal sbt project instead of play?

scalaplayframeworksbt

提问by pamu

When i tried using Play WS library in a normal sbt project instead of play project I was bound to use play.api.Play.current and gotjava.lang.RuntimeException: "There is no started application"when tried to run the application.

当我尝试在普通 sbt 项目而不是 play 项目中使用 Play WS 库时,我必须使用 play.api.Play.current 并在尝试运行该应用程序时得到java.lang.RuntimeException: "There is no started application"

回答by implicitdef

Usage in 2.4.x

在 2.4.x 中的用法

import play.api.libs.ws.ning.NingWSClient   

val wsClient = NingWSClient()
wsClient.url("http://wwww.something.com").get()

build.sbt :

构建.sbt:

libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.4.3"

Usage in 2.5.x

在 2.5.x 中的用法

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 :

构建.sbt:

libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.5.4"

Logs

日志

As someone noted in the comment, by default you might get a bunch of verbose logs coming from the underlying async-http-client. One way to fix it is to start configuring a logback.xml, and placing it in src/main/resources

正如有人在评论中指出的那样,默认情况下,您可能会从底层 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>

回答by planetenkiller

To use play-ws outside of play see "Using WSClient" section of documentation: http://www.playframework.com/documentation/2.3.x/ScalaWS

要在游戏之外使用 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()