json之大体的 POST 请求在服务器端收到空

powertoolsteam 阅读:48 2024-09-07 23:24:14 评论:0

当向我的 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 中同时使用 Advanced REST Client 和 ċurl 来发送请求,但都失败了。

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

请您参考如下方法:

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

Max content length

Text based body parsers (such as text, json, xml or formUrlEncoded) use a max content length because they have to load all the content into memory.

There is a default content length (the default is 100KB).

Tip: The default content size can be defined in application.conf:

parsers.text.maxLength=128K

You can also specify a maximum content length via the @BodyParser.Of annotation:

// 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());  
  } 
} 


标签:json
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号