A query on code snippet

On Lagom site, following code snippet is provided:

@Override
  public ServiceCall<NotUsed, String> hello(String id) {
    return request -> {
      // Look up the hello world entity for the given ID.
      PersistentEntityRef<HelloCommand> ref = persistentEntityRegistry.refFor(HelloWorld.class, id);
      // Ask the entity the Hello command.
      return ref.ask(new Hello(id, Optional.empty()));
    };
  }

I have a query here.
Why do we have parametrized ServiceCall with NotUsed when we do make use of id in forming our response?

The input type of a ServiceCall is the request payload. The id your passing comes from the path.

For instance:

pathCall("/api/hello/:id", hello _)

When you make a call to /api/hello/Joe, id will be “Joe”, but there is not request payload in that case.
This is typically the case for GET call in which you don’t have a request payload.

If you define your ServiceCall as ServiceCall<User, String>, you can only do a POST or PUT and you will need to pass User in the payload, probably serialised as json.

Cheers,

Renato

1 Like