Java HttpServletRequest 获取 JSON POST 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3831680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpServletRequest get JSON POST data
提问by Don Ch
I am HTTP POST-ing to URL http://laptop:8080/apollo/services/rpc?cmd=execute
我正在 HTTP POST 到 URL http://laptop:8080/apollo/services/rpc?cmd=execute
with POST data
带有 POST 数据
{ "jsondata" : "data" }
Http request has Content-Type of application/json; charset=UTF-8
Http 请求的 Content-Type 为 application/json; charset=UTF-8
How do I get the POST data (jsondata) from HttpServletRequest?
如何从 HttpServletRequest 获取 POST 数据 (jsondata)?
If I enumerate the request params, I can only see one param, which is "cmd", not the POST data.
如果我枚举请求参数,我只能看到一个参数,即“cmd”,而不是 POST 数据。
回答by CharlesLeaf
Are you posting from a different source (so different port, or hostname)? If so, this very very recent topic I just answered might be helpful.
您是从不同的来源(如此不同的端口或主机名)发帖吗?如果是这样,我刚刚回答的这个最近的话题可能会有所帮助。
The problem was the XHR Cross Domain Policy, and a useful tip on how to get around it by using a technique called JSONP. The big downside is that JSONP does not support POST requests.
问题在于 XHR 跨域策略,以及有关如何使用称为 JSONP 的技术绕过它的有用提示。最大的缺点是 JSONP 不支持 POST 请求。
I know in the original post there is no mention of JavaScript, however JSON is usually used for JavaScript so that's why I jumped to that conclusion
我知道在原帖中没有提到 JavaScript,但是 JSON 通常用于 JavaScript,所以这就是我得出这个结论的原因
回答by Kdeveloper
Normaly you can GET and POST parameters in a servlet the same way:
通常,您可以以相同的方式在 servlet 中 GET 和 POST 参数:
request.getParameter("cmd");
But only if the POST data is encodedas key-value pairs of content type: "application/x-www-form-urlencoded" like when you use a standard HTML form.
但仅当 POST 数据被编码为内容类型的键值对时:“application/x-www-form-urlencoded”就像您使用标准 HTML 表单一样。
If you use a different encoding schema for your post data, as in your case when you post a json data stream, you need to use a custom decoder that can process the raw datastream from:
如果您对发布数据使用不同的编码模式,就像您发布json 数据流的情况一样,您需要使用可以处理来自以下位置的原始数据流的自定义解码器:
BufferedReader reader = request.getReader();
Json post processing example (uses org.jsonpackage )
Json 后处理示例(使用org.json包)
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { /*report an error*/ }
try {
JSONObject jsonObject = HTTP.toJSONObject(jb.toString());
} catch (JSONException e) {
// crash and burn
throw new IOException("Error parsing JSON request string");
}
// Work with the data using methods like...
// int someInt = jsonObject.getInt("intParamName");
// String someString = jsonObject.getString("stringParamName");
// JSONObject nestedObj = jsonObject.getJSONObject("nestedObjName");
// JSONArray arr = jsonObject.getJSONArray("arrayParamName");
// etc...
}