简体   繁体   中英

How to make request param as optional in a rest api path in Lagom?

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?

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);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM