2014-12-26 34 views
0

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

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

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

回答

0

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