lagom.javadsl.api.Descriptor 中的 restcall、pathcall、namedcall 是什么;

what is restcall,pathcall,namedcall in lagom.javadsl.api.Descriptor;

我是微服务和 Lagom 框架的新手,在我们进行 ServiceCalls 的服务 api 中,我不明白 namedcallpathcallrestcall。我们应该在何时何地使用哪个?

例如在这些调用中:

  ServiceCall<NotUsed, Cargo, Done> register();
  restCall(Method.POST, "/api/registration", register()

  ServiceCall<NotUsed, NotUsed, Source<Cargo, ?>> getLiveRegistrations();
  pathCall("/api/registration/live", getLiveRegistrations())

  ServiceCall<User, NotUsed> createUser();
  namedCall("/api/users", this::createUser)

Lagom Documentation 涵盖得很好:

  1. 已命名

The simplest type of identifier is a name, and by default, that name is set to be the same name as the name of the method on the interface that implements it. A custom name can also be supplied, by passing it to the namedCall method:

default Descriptor descriptor() {
    return named("hello").withCalls(
        namedCall("hello", this::sayHello)
    );
}

In this case, we’ve named it hello, instead of the default of sayHello. When implemented using REST, this will mean this call will have a path of /hello.

  1. 路径

The second type of identifier is a path based identifier. This uses a URI path and query string to route calls, and from it dynamic path parameters can optionally be extracted out. They can be configured using the pathCall method.

Dynamic path parameters are extracted from the path by declaring dynamic parts in the path. These are prefixed with a colon, for example, a path of /order/:id has a dynamic part called id. Lagom will extract this parameter from the path, and pass it to the service call method.

ServiceCall<NotUsed, Order> getOrder(long id);

default Descriptor descriptor() {
    return named("orders").withCalls(
        pathCall("/order/:id", this::getOrder)
    );
}

Multiple parameters can of course be extracted out, these will be passed to your service call method in the order they are extracted from the URL:

  1. 休息电话

最后一种标识符是 REST 标识符。 REST 标识符旨在在创建语义 REST API 时使用。它们同时使用路径(与基于路径的标识符一样)和请求方法来识别它们。它们可以使用 restCall 方法进行配置:

ServiceCall<Item, NotUsed> addItem(long orderId);
ServiceCall<NotUsed, Item> getItem(long orderId, String itemId);
ServiceCall<NotUsed, NotUsed> deleteItem(long orderId, String itemId);

default Descriptor descriptor() {
return named("orders").withCalls(
        restCall(Method.POST,   "/order/:orderId/item",         this::addItem),
        restCall(Method.GET,    "/order/:orderId/item/:itemId", this::getItem),
        restCall(Method.DELETE, "/order/:orderId/item/:itemId", this::deleteItem)
);
}