简体   繁体   中英

How to inject WSClient in a Play framework project when using Maven?

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:

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.

Looking at the 2.6 migration guide we can read:

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 [...]

So to resolve the issue we have to add the play-ahc-ws module to the Maven's pom.xml:

<dependency>
  <groupId>com.typesafe.play</groupId>
  <artifactId>play-ahc-ws_2.12</artifactId>
  <version>${play2.version}</version>
</dependency>

If using Guice like in the code sample, the dependency injection will be handled by Guice.

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