如何传递到控制器方法请求主体?

问题描述 投票:0回答:1

我正在使用play framework 2.8.x,我需要传递到控制器请求正文。我想做这样的事情:

public class HomeController extends Controller {
    public Result test(String tokenId) {
        ...
        return ok();
    }
}

并发送如下请求:enter image description here

我该怎么做?

java playframework
1个回答
0
投票

您需要从Request对象获取它,例如:

@BodyParser.Of(BodyParser.Json.class)
public Result index(Http.Request request) {
  JsonNode json = request.body().asJson();
  String tokenId = json .get("tokenId ").asText();
  ...
  return ok();
}

请参阅官方文档以获取详细信息:https://www.playframework.com/documentation/2.8.x/JavaBodyParsers#Body-parsers

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.