Is it possible to bind path variables to a domain object in Lagom?

Is it possible to bind path variables to a domain object in Lagom?

Yes, Lagom provides an interface called PathParamSerializer that can be used to convert a path parameter to another type. In fact, there are already many built-in path parameter serializers at your disposal. However, there are still a number of primitives that are not included which would require this custom implementation*.

Allthough, note that if your object takes more than one parameter the  you should be aware of a limitation where Lagom expects each path parameter to convert to a single method parameter. This means that there is no built in way to take separate parameters and convert them into a single parameter that wraps both of them. To workaround this you can follow one of these two patterns:

Option 1: Define each parameter separately in your service call
ServiceCall<NotUsed, GetPersonResponse> getPerson(float id, float id2);

@Override
default Descriptor descriptor() {
  return named("getPerson")
           .withCalls(restCall(GET,  "/getPerson/id/:id/id2/:id2", this::getPerson))
           .withPathParamSerializer(Float.class,
PathParamSerializers.required("Float", String::parseFloat, Object::toString))
           .withAutoAcl(true);
}
This can then be called with the URL http://localhost:9000/getPerson/id/1.23/id2/4.56.

Option 2: Define a single path parameter that is parsed by the PathParamSerializer
ServiceCall<NotUsed, GetPersonResponse> getPerson(IdHolder ids);

@Override
default Descriptor descriptor() {
    return named("getPerson")
             .withCalls(restCall(GET, "/getPerson/:ids", this::getPerson))
             .withPathParamSerializer(
               IdHolder.class,
               PathParamSerializers.required(
                 "IdHolder", idHolderString -> {
                   String[] splitIds = idHolderString.split(",");
                   if (splitIds.length != 2) {
                     throw new IllegalArgumentException(
                       "Expected two ids but got " + splitIds.length + " in " + idHolderString);
                   }
                   return new IdHolder(
                     Float.parseFloat(splitIds[0]), // id
                     Float.parseFloat(splitIds[1])  // id2
                   );
                 },
                 ids -> ids.getId() + "," + ids.getId2()
               )
            )  
            .withAutoAcl(true);
}

This can then be called with the URL http://localhost:9000/getPerson/1.23,4.56The custom path parameter serializer takes care of converting back and forth between the Ids and a comma-separated string. The example uses lambdas for brevity, but you might prefer to add methods to the class itself, even adding any custom validation that could be needed.

*If you do end up implementing a primitive's serializer using the aforementioned interface then we are always open to integrating it for other users as part of a project contribution. Alternatively, you could provide us with the code so that we can push it through ourselves.

    • Related Articles

    • Should Kafka act as the source of truth in Lagom?

      > We are using Lagom with event sourcing and CQRS. Would you say that keeping the source of truth on the Kafka side is an option? Pragmatism always wins the day, and with that being said this idea has plenty of merit. The only concern we have with ...
    • Does Lagom support multitenancy?

      Our suggestion is to not go down this path. Multi-tenancy was very popular back in the time when people used to deploy one single application on a huge application server. In the era of cloud computing, it's much easier to have different deployments, ...
    • Can I customize error handling in Lagom?

      Yes, the best way to customize error handling in Lagom is to use a custom ExceptionSerializer.  This Knoldus blog post explains it well: https://blog.knoldus.com/2018/02/05/exception-serializer-in-lagom/ That example, however, is more focused on how ...
    • Possible causes of 'Ask timed out on [...] after [# ms]. Sender[null] sent message of type "com.lightbend.lagom.scaladsl.persistence.CommandEnvelope"'?

      This message means that the actor holding the PersistentEntity didn’t respond on time. It happens when the Future returned by the ask method didn’t complete after #ms. This can be caused by many different reasons. You have to consider it as the ...
    • How to implement versioning in Lagom microservices?

      Lagom doesn't provide any solution, plugin or feature that enables API versioning. However you can use the following to decide and pick what works best for you: When versioning you must keep in mind both the service endpoints and payloads and the ...