Lagom: Consuming a service part-2

Table of contents
Reading Time: 2 minutes

So here’s the situation: We are using Lagom framework to develop our micro-services and we need to consume data from other services. What should we do?

Well, it’s not going to be a problem for us. Lagom provides very easy way to consume a service. The very first way to consume a service is to use the unmanagedServices parameter provided in Lagom plugin. To understand the use of unmanagedServices, please refer to the following link.

Now wait a second, In the above blog, we have created an interface for the unmanaged service that could be used to communicate with the service from which we are trying to consume data.
But if we are consuming a service that is already implemented in Lagom way, wouldn’t that be a boilerplate for us? Of Course it is. So what should we do now?
Here’s the solution from Lagom:
Prerequisite: The service from which you want to consume the data should be published via a build.
Now let’s consume the service: The following 4 steps will help you if you need to consume from another Lagom micro-service.

Step 1: The lagom-maven-plugin offers a configuration item called externalProjects that can be configured on the root project to import external projects into a Maven build.



Step 2: The dependency of the api module of the service we need to consume is to be added in the impl of the service we are implementing.


Now the question arises is, how would Lagom know that it has to provide an implementation for the unmanaged service’s api?
And the answer is, “Binding the service client ”

Step 3: Provide a binding for the client i.e, the unmanaged service.

protected void configure() 
    bindService(UnmanagedExampleService.class, UnmanagedExampleServiceImpl.class);


Step 4: Now it’s time to use the service. And to do so, we will have to inject the client into our component using @Inject annotation.

public class UnmanagedExampleServiceImpl implements UnmanagedExampleService {

    private final HelloService helloService;

    UnmanagedExampleServiceImpl(HelloService helloService) {
        this.helloService = helloService;

    public ServiceCall showDataFromUnmanagedService() {
        return request -> {
            String responseString;
            Data data = null;
            ObjectMapper jsonMapper = new ObjectMapper();
            try {
                LinkedHashMap response = helloService.getResultFromUnManagedService().invoke().toCompletableFuture
                responseString = jsonMapper.writeValueAsString(response);
                data = jsonMapper.readValue(responseString, Data.class);
            } catch (Exception ex) {
            return CompletableFuture.completedFuture(data);


So this was an example to show the flow that has to be followed when an unmanaged service (already implemented using Lagom framework) has to be integrated in a Lagom project. Let’s have a quick recap: First, register the external project in pom.xml. Then, add the dependency of the api module of the service to be consumed. Then, bind the interface using bindClient. And in the end, inject the client in your component to call it’s methods and consume the response.
Hope this attempt would be helpful to you. For more doubts and examples regarding Lagom, feel free to go through our blogs, because we at Knoldus believe in gaining knowledge and growing our skills together.


Pasted image at 2017_11_27 04_17 PM

Written by 

Anmol Mehta is a Software Consultant having experience of more than 1.5 years. A keen programmer who has experience in Scala and Java. He is recognized as a dedicated and determined team player who enjoys working on new technologies. He is a professional and a technology enthusiast. He believes in the approach that “teamwork makes the dream work”. He is a quick and always-on learner who ensures better quality in every increment of his work. He believes in abiding standard coding practices. He always looks after that the team is working in sync with each other both at the technical and managerial level.

2 thoughts on “Lagom: Consuming a service part-23 min read

  1. Hi Anmol ,can you please share the git repo for the whole code ,I am not still not sure , what code should go in UnmanagedService . I have put restCall(Method.GET,”/api/hello/:id”,this::hello). I want to know what code should go in the ” hello ” method

Comments are closed.