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.