Java Spark Framework - 在中间件之后请求丢失

时间:2014-12-26 12:58:11

标签: java spark-java

我正在为我的应用程序使用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       
});

但请求体突然变空(其他属性和标题保持不变)。

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

1 个答案:

答案 0 :(得分:0)

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