【问题标题】:Java Spark Framework - request body lost after middlewareJava Spark 框架 - 中间件后请求正文丢失
【发布时间】:2014-12-26 12:58:10
【问题描述】:

我正在为我的应用程序使用 Spark 框架。

我有一个中间件,它检查(除其他外)正文是否为 JSON 格式:

    // Middleware
    before((req, res) -> {
        // Method check
        if (!req.requestMethod().equals("POST")) {
            halt(403, "{\"result\":\"ERR\",\"errMsg\":\"Only POST allowed!\",\"code\":403}");
        }
        // JSON Check
        JSONObject body_json = new JSONObject();
        try {
            body_json = new JSONObject(req.body());
        } catch (JSONException e) {
            halt(403, "{\"result\":\"ERR\",\"errMsg\":\"No valid JSON!\",\"code\":403}");
        }
        // At this point (end of middleware) the request body is still unchanged !
    });

然后我就有了处理 POST 请求的正常功能:

post("/post_some_data", (req, res) -> {
     String body = req.body()           // This string is empty !!
     int length = req.contentLength();  // This remain unchanged       
});

但是请求体突然变空(其他属性和头部保持不变)。

这是一个错误还是我做错了什么??

【问题讨论】:

    标签: java spark-java


    【解决方案1】:

    spark 框架中有一个bug。将库更新到 2.1 版本将解决这个问题以及所有类似问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-05
      • 2015-02-14
      • 1970-01-01
      • 2022-01-02
      • 2019-01-10
      • 2019-03-13
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多