gpt4 book ai didi

Java Spark框架-中间件后请求正文丢失

转载 作者:行者123 更新时间:2023-12-01 12:05:29 24 4
gpt4 key购买 nike

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

但是请求正文突然变空(其他属性和 header 保持不变)。

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

最佳答案

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

关于Java Spark框架-中间件后请求正文丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27657488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com