无法使用注入器注入Play的WSClient实例

时间:2017-07-12 20:32:07

标签: java scala playframework playframework-2.0 scalatest

我有一个注入了WSClient依赖的类:

@Singleton
class MyApiService @Inject() (wsclient: WSClient, conf: Configuration) {
...
}

当使用进样器运行测试并创建MyApiService实例时:

class MyTest extends FreeSpec with OneAppPerSuite with ScalaFutures with WsScalaTestClient {

  implicit lazy val materializer: Materializer = app.materializer

  lazy val wsc: WSClient = app.injector.instanceOf[WSClient]
  lazy val conf: Configuration = app.injector.instanceOf[Configuration]

  val myApiService: MyApiService = app.injector.instanceOf[MyApiService]

  "Api Test" in {
    ...
  }

我收到此错误:

  
    

导致中止运行的异常或错误:无法配置,请参阅     以下错误:

         

1)注入构造函数时出错,java.lang.NumberFormatException:     格式错误10000 at     。play.api.libs.ws.ahc.AsyncHttpClientProvider(AhcWSModule.scala:40)     在     play.api.libs.ws.ahc.AsyncHttpClientProvider.class(AhcWSModule.scala:39)     同时找到play.api.libs.ws.ahc.AsyncHttpClientProvider     找到play.shaded.ahc.org。 ...引起:     java.lang.NumberFormatException:format error 10000

  

并在我的application.cong中添加了:

  ws.timeout.connection = 10000
  ws.timeout.idle = 10000
  ws.timeout.request = 10000

尝试改为60000,没有区别......

使用play 2.6.0和scala 2.11.8

有人可能知道为什么会失败?

感谢

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。这是ws.timeout属性的格式问题。

在Play 2.6中,这些属性需要具有时间单位。这是example configuration

WSConfigParser尝试使用这些属性中的值构造scala.concurrent.Duration的实例。如果提供的值没有有效的时间单位,则持续时间为throw an exception

修复方法是将1000ms添加到这些属性中 - > play { ws { timeout { connection: 10000ms idle: 30000ms request: 60000ms } } }

这是我的application.conf

Qt.Dialog | Qt.WindowCancelButtonHint | Qt.WindowCloseButtonHint

答案 1 :(得分:0)

我认为这可能与此问题有关:https://github.com/playframework/playframework/issues/7056#issuecomment-285370901

建议的解决方法: 创建文件 /conf/ahc-default.properties包含此gist https://gist.github.com/domdorn/3c80fac337ffc847650ae5f547f62c55

的内容