【问题标题】:POST Request in Playframework JavaPlay Framework Java 中的 POST 请求
【发布时间】:2019-04-24 16:29:50
【问题描述】:

嗨,在我的 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());
}

所以现在编译器返回:

类 Index 中的方法 printName 缺少参数; 如果您想将其视为部分应用的函数,请在此方法后面加上 `_'

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

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

然后我得到了这个错误

未找到:键入请求

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

提前致谢。

【问题讨论】:

    标签: java playframework


    【解决方案1】:

    我找到了解决办法:

    控制器功能

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 2017-09-03
      • 2017-01-04
      • 2020-07-29
      相关资源
      最近更新 更多