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:
/api/users
(for this, the string filter in the handler should be null or empty) /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:
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.