从 HttpServletRequest 检索 JSON 对象文字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1548782/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:20:15  来源:igfitidea点击:

Retrieving JSON Object Literal from HttpServletRequest

jsonpostservlets

提问by DarthMaul

I am writing code that needs to extract an object literal posted to a servlet. I have studied the API for the HttpServletRequest object, but it is not clear to me how to get the JSON object out of the request since it is not posted from a form element on a web page.

我正在编写需要提取发布到 servlet 的对象文字的代码。我已经研究了 HttpServletRequest 对象的 API,但我不清楚如何从请求中获取 JSON 对象,因为它不是从网页上的表单元素发布的。

Any insight is appreciated.

任何见解表示赞赏。

Thanks.

谢谢。

回答by user305224

are you looking for this ?

你在找这个吗?

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = request.getReader();
    try {
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } finally {
        reader.close();
    }
    System.out.println(sb.toString());
}

回答by Edd

The easiest way is to populate your bean would be from a Reader object, this can be done in a single call:

最简单的方法是从 Reader 对象填充 bean,这可以通过一次调用完成:

BufferedReader reader = request.getReader();
Gson gson = new Gson();

MyBean myBean = gson.fromJson(reader, MyBean.class);

回答by Clyde D'Cruz

make use of the Hymanson JSON processor

使用 Hymanson JSON 处理器

 ObjectMapper mapper = new ObjectMapper();
  Book book = mapper.readValue(request.getInputStream(),Book.class);

回答by Dmitry Stolbov

This is simple method to get request data from HttpServletRequestusing Java 8 Stream API:

这是HttpServletRequest使用Java 8 Stream API获取请求数据的简单方法:

String requestData = request.getReader().lines().collect(Collectors.joining());

回答by xedo

There is another way to do it, using org.apache.commons.io.IOUtilsto extract the String from the request

还有另一种方法可以做到,使用org.apache.commons.io.IOUtils从请求中提取字符串

String jsonString = IOUtils.toString(request.getInputStream());

Then you can do whatever you want, convert it to JSONor other object with Gson, etc.

然后你可以做任何你想做的事情,将它转换为JSON或其他对象Gson,等等。

JSONObject json = new JSONObject(jsonString);
MyObject myObject = new Gson().fromJson(jsonString, MyObject.class);

回答by arunjitsingh

If you're trying to get data out of the request body, the code above works. But, I think you are having the same problem I was..

如果您尝试从请求正文中获取数据,则上面的代码有效。但是,我认为您遇到了与我相同的问题..

If the data in the body is in JSON form, and you want it as a Java object, you'll need to parse it yourself, or use a library like google-gsonto handle it for you. You should look at the docs and examples at the project's website to know how to use it. It's fairly simple.

如果正文中的数据是 JSON 形式,而您希望将其作为 Java 对象,则需要自己解析它,或者使用google-gson 之类的库来为您处理。您应该查看项目网站上的文档和示例以了解如何使用它。这相当简单。

回答by Ashok

Converting the retreived data from the request object to json object is as below using google-gson

使用google-gson将检索到的数据从请求对象转换为json对象如下

Gson gson = new Gson();
ABCClass c1 = gson.fromJson(data, ABCClass.class);

//ABC class is a class whose strcuture matches to the data variable retrieved