Search code examples
javalagom

What is the simplest way to consume an external REST service in Lagom?


According to Lagom documentation, we can define external service URI (like below) and can get it from ServiceLocator.

lagomUnmanagedServices in ThisBuild := Map("weather" -> "http://localhost:3333")

http://www.lagomframework.com/documentation/1.0.x/ServiceLocator.html#Communicating-with-external-services

What is the simplest way to call the external REST API in Lagom?

I considered using WsClient in Lagom, but I didn't choose it. Lagom includes only WsClient for Scala, therefore it provides result values as not java.util.concurrent.CompletionStage but scala.concurrent.Future. It makes the pain to combine with other Lagom APIs like CompletionStage#doWithService.


Solution

  • A way to consume 3rd party REST services from lagom is by writing the 3rd party's REST spec using a Lagom Descriptor.

    Imagine your code wanted to interact with Slack's API, you would create a slack-api project in your app and create the Slack descriptor there (you wouldn't need to create a slack-implof course).

    Then, on your fancy-impl code you would depend on slack-api and in your FancyServiceImpl implementation you would inject SlackService in the constructor.

    PS: The gist is scala code, but same idea applies to Lagom's Java DSL.