0

I want to make the request parameter optional in the path of the rest Call method. Suppose the service descriptor is

public interface UserService extends Service {

    ServiceCall<NotUsed, PSequence<User>> getUsers(String filter);

    @Override
    default Descriptor descriptor() {
        return Service.named("user-service").withCalls(
                Service.restCall(Method.GET, "/api/users", 
                                                 this::getUsers)
        ).withAutoAcl(true);
    }
}

I want to use the the same handler for two different urls, one with request param and one without request param.

For example:

  1. /api/users (for this, the string filter in the handler should be null or empty)
  2. /api/users?filter=abc (for this, the value of filter should be abc).

Is this possible?

Jatin
  • 31,116
  • 15
  • 98
  • 163
konghoho
  • 53
  • 6

1 Answers1

2

Yes, use an java.util.Optional<String> type, and the syntax for query strings documented here:

https://www.lagomframework.com/documentation/1.3.x/java/ServiceDescriptors.html#Path-based-identifiers

So:

public interface UserService extends Service {

  ServiceCall<NotUsed, PSequence<User>> getUsers(Optional<String> filter);

  @Override
  default Descriptor descriptor() {
    return Service.named("user-service").withCalls(
      Service.restCall(Method.GET, "/api/users?filter", this::getUsers)
    ).withAutoAcl(true);
  }
}
James Roper
  • 12,695
  • 46
  • 45