POST 在 Playframework 中请求 Java

POST Request in Playframework Java

您好,在我的 playframework 应用程序中,我想做一个简单的 Post 请求。

所以我在我的路线中定义了这个:

POST        /printName                              @controllers.Index.printName()

我在 scala 中使用的方法相同。

那么我有如下控制器功能:

public Result printName(Http.Request request) {
    JsonNode json = request.body().asJson();
    return ok("Got name: " + json.get("name").asText());
}

所以现在编译器 returns:

missing arguments for method printName in class Index; follow this method with `_' if you want to treat it as a partially applied function

当我像这样在路由中添加参数时:

POST        /printName                  @controllers.Index.printName(request: Request)

然后我得到这个错误

not found: type Request

如何正确?示例来自 Playframework 页面:https://www.playframework.com/documentation/2.7.x/JavaBodyParsers#The-default-body-parser

提前致谢。

我找到了解决方案:

控制器功能

public Result printName() {
    Http.Request request = request();
    JsonNode json = request.body().asJson();
    return ok("Got name: " + json.get("name").asText());
}

和路线

POST        /printName              @controllers.Index.printName()