如何在Scala应用程序中使用独立的WSClient

时间:2018-05-27 15:40:05

标签: scala playframework

我想在独立应用程序中使用ws。尝试从https://gist.github.com/cdimascio/46b2b7d2986636c1189c复制此代码:

import com.ning.http.client.AsyncHttpClientConfig
import play.api.libs.ws.ning._
import play.api.libs.ws._

// provide an execution context
import scala.concurrent.ExecutionContext.Implicits.global

object WSStandaloneTest {

  def main(args: Array[String]) {
    // set up the client
    val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build
    val builder = new AsyncHttpClientConfig.Builder(config)
    val client = new NingWSClient(builder.build)

    // execute a GET request
    val response = client.url("http://www.example.com").get

    // print the response body
    response.foreach(r => {
      println(r.body) 
      // not the best place to close the client, 
      // but it ensures we dont close the threads before the response arrives 
      // Good enough for the gist :-D
      client.close()
    })
  }
}

导致以下错误:

[error] object ning is not a member of package play.api.libs.ws
[error] import play.api.libs.ws.ning._

在我的build.sbt我有这个:

libraryDependencies += "com.typesafe.play" %% "play-json" % "2.6.1"
libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.6.1"

我做错了什么?

1 个答案:

答案 0 :(得分:1)

Play中不推荐使用

NingWSClient! 2.5.X。

在2.6.x中

  

ning包已被ahc包替换,Ning *类被AHC *替换。

official doc中提供了迁移指南。

因此,您可以选择降级到2.5.x并使用ning或更新代码。