3

当向POST我的 Play 框架操作方法发出具有大主体的请求时,我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 中使用 Advanced REST Client 并ċurl发送请求,但都失败了。

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

4

1 回答 1

6

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

最大内容长度

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

有一个默认的内容长度(默认为 100KB)。

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

parser.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()); 
  }
}
于 2014-01-05T16:59:42.130 回答