Failing to inject Play's WSClient instance using injector

♀尐吖头ヾ 提交于 2019-12-10 16:37:22

问题


I have a Class that has a WSClient dependancy injected:

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

and when running test and creating instance of MyApiService using injector :

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 {
    ...
  }

I get this error:

An exception or error caused a run to abort: Unable to provision, see the following errors:

1) Error injecting constructor, java.lang.NumberFormatException: format error 10000 at play.api.libs.ws.ahc.AsyncHttpClientProvider.(AhcWSModule.scala:40) at play.api.libs.ws.ahc.AsyncHttpClientProvider.class(AhcWSModule.scala:39) while locating play.api.libs.ws.ahc.AsyncHttpClientProvider while locating play.shaded.ahc.org. ... Caused by: java.lang.NumberFormatException: format error 10000

and in my application.cong I added:

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

tried to change to 60000 theres no difference...

using play 2.6.0 and scala 2.11.8

someone maybe know why is it failing?

thanks


回答1:


I was having this same problem. It is a formatting issue with the ws.timeout properties.

In Play 2.6, those properties are required to have a time unit. Here is an example configuration

WSConfigParser tries to construct an instance of scala.concurrent.Duration with the values from those properties. Duration will throw an exception if the values provided do not have a valid time unit.

The fix is to add ms to these properties -> 1000ms

This is my application.conf

play {
  ws {
    timeout {
      connection: 10000ms
      idle: 30000ms
      request: 60000ms
    }
  }
}



回答2:


I think it might relate to this issue: https://github.com/playframework/playframework/issues/7056#issuecomment-285370901

There is a suggested workaround: create the file /conf/ahc-default.properties with the contents of this gist https://gist.github.com/domdorn/3c80fac337ffc847650ae5f547f62c55



来源:https://stackoverflow.com/questions/45067001/failing-to-inject-plays-wsclient-instance-using-injector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!