服务器端的大型机身POST请求为空

时间:2014-01-05 16:29:52

标签: json playframework http-post playframework-2.2 http-content-length

当我的Play框架操作方法发出一个大型POST请求时,我在提取数据时得到null。如果身体相当小,我可以很好地检索数据。

以下是一个简短的数据集示例:

{
  "creator": "zoltan",
  "sport": "hike",
  "geometry": [
    {
      "time": "2009-07-10 12:56:10 +0000",
      "x": 10.275514,
      "y": 47.514749,
      "z": 756.587
    },
    {
      "time": "2009-07-10 12:56:19 +0000",
      "x": 10.275563,
      "y": 47.514797,
      "z": 757.417
    }
  ]
}

当我在正文中发出带有此JSON的POST请求时,一切正常。但是,如果我在geometry数组中添加更多(~4000)个点,我会在操作中获得null

这是我的行动方法:

@Transactional
//@BodyParser.Of(Json.class) // tried with this as well
public static Result createTour() {
    LOG.debug("Raw request body: " + request().body().asText());
    JsonNode node = request().body().asJson();
    LOG.debug("JSON request body: " + node);
    TourDto tourDto;
    try {
        tourDto = jsonToTour(node);
        int id = TourDataAccessUtils.create(tourDto);
        return created(toJson(id));
    } catch (JsonProcessingException e) {
        LOG.error("While parsing JSON request.", e);
        return Results.badRequest(
                toJson(Throwables.getRootCause(e).getMessage()));
    }
}

我尝试使用chrome和ċurl中的高级REST客户端发送请求,但都失败了。

可能是什么问题?可能是我需要为大请求包含Content-Lenght标头吗?如果是这样,我如何为任意JSON数据手动计算它?

1 个答案:

答案 0 :(得分:6)

请检查PlayFramework documentation,他们提到请求的默认最大长度为100KB:

  

最大内容长度

     

基于文本的正文解析器(例如text,json,xml或   formUrlEncoded)使用最大内容长度,因为它们必须加载所有内容   内容进入记忆。

     

默认内容长度(默认为100KB)。

     

提示:可以在application.conf中定义默认内容大小:

     

parsers.text.maxLength = 128K

     

您还可以通过@ BodyParser.Of指定最大内容长度   注释:

// Accept only 10KB of data.
@BodyParser.Of(value = BodyParser.Text.class, maxLength = 10 * 1024)
pulic static Result index() {
  if(request().body().isMaxSizeExceeded()) {
    return badRequest("Too much data!");
  } else {
    ok("Got body: " + request().body().asText()); 
  }
}
相关问题