При создании проекта платформы Play и использовании WSClient для вызова REST официальная документация по платформе Play предлагает добавить ws к build.sbt для управления зависимостями. При использовании Maven зависимость ws включена в:
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-ws_2.12</artifactId>
<version>${play2.version}</version>
</dependency>
Однако при попытке вызвать в веб-службу с помощью такого фрагмента:
@Singleton
class Controller @Inject()(
ws: WSClient,
controllerComponents: ControllerComponents
)(implicit ec: ExecutionContext)
extends AbstractController(controllerComponents) {
def callApi(): Action[AnyContent] = Action.async { _ =>
ws
.url("https://mywebservice.com/api/bla")
.get()
.map(response => Ok(response.body.toString))
}
}
Затем появляется следующая ошибка:
CreationException: Unable to create injector, see the following errors:
1) No implementation for play.api.libs.ws.WSClient was bound.
while locating play.api.libs.ws.WSClient
for the 1st parameter of controllers.MyController.<init>(MyController.scala:13)
while locating controllers.MyController
for the 3rd parameter of router.Routes.<init>(Routes.scala:33)
at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:123):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4)





Как говорится в документации:
Note: In Play 2.6, Play WS has been split into two, with an underlying standalone client that does not depend on Play, and a wrapper on top that uses Play specific classes. In addition, shaded versions of AsyncHttpClient and Netty are now used in Play WS to minimize library conflicts, primarily so that Play’s HTTP engine can use a different version of Netty. Please see the 2.6 migration guide for more information.
Глядя на руководство по миграции 2.6, мы можем прочитать:
If you have a Play SBT project, you can still add WS by adding the following line to your build.sbt:
libraryDependencies += ws
This includes the play-ahc-ws module [...]
Итак, чтобы решить эту проблему, мы должны добавить модуль играть-ahc-ws в pom.xml Maven:
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-ahc-ws_2.12</artifactId>
<version>${play2.version}</version>
</dependency>
При использовании Guice, как в примере кода, внедрение зависимостей будет выполняться Guice.