无法使用注入器注入Play的WSClient实例 [英] Failing to inject Play's WSClient instance using injector
问题描述
我有一个注入了WSClient依赖项的类:
I have a Class that has a WSClient dependancy injected:
@Singleton
class MyApiService @Inject() (wsclient: WSClient, conf: Configuration) {
...
}
以及在运行测试并使用注入器创建MyApiService实例时:
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 {
...
}
我收到此错误:
异常或错误导致运行中止:无法配置,请参阅 以下错误:
An exception or error caused a run to abort: Unable to provision, see the following errors:
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:格式错误10000
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
,然后在我的application.cong
中添加:
ws.timeout.connection = 10000
ws.timeout.idle = 10000
ws.timeout.request = 10000
试图更改为60000,没有区别...
tried to change to 60000 theres no difference...
使用play 2.6.0和scala 2.11.8
using play 2.6.0 and scala 2.11.8
也许有人知道为什么会失败?
someone maybe know why is it failing?
谢谢
推荐答案
我遇到了同样的问题.这是ws.timeout属性的格式问题.
I was having this same problem. It is a formatting issue with the ws.timeout properties.
在Play 2.6中,这些属性必须具有时间单位.这是一个示例配置
In Play 2.6, those properties are required to have a time unit. Here is an example configuration
WSConfigParser尝试使用这些属性中的值构造一个scala.concurrent.Duration实例.持续时间将引发异常如果提供的值没有有效的时间单位.
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.
解决方法是将ms
添加到这些属性-> 1000ms
The fix is to add ms
to these properties -> 1000ms
这是我的application.conf
This is my application.conf
play {
ws {
timeout {
connection: 10000ms
idle: 30000ms
request: 60000ms
}
}
}
这篇关于无法使用注入器注入Play的WSClient实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!