简体   繁体   中英

Failing to inject Play's WSClient instance using injector

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM