2014-12-26 35 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版本將解決此問題和所有類似問題。