When creating a Play framework project and using WSClient to make REST call, the official Play framework documentation suggests to add ws to the build.sbt to manage dependencies. If using Maven the ws dependency is included in:
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-ws_2.12</artifactId>
<version>${play2.version}</version>
</dependency>
However when trying to make a call to a web service using a snipped like this:
@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))
}
}
Then the following error appears:
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)
As the documentation says:
Looking at the 2.6 migration guide we can read:
So to resolve the issue we have to add the play-ahc-ws module to the Maven's pom.xml:
If using Guice like in the code sample, the dependency injection will be handled by Guice.