67

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.

Any insight is appreciated.

Thanks.

DarthMaul
  • 719
  • 1
  • 5
  • 5
  • 1
    Your question is confusing. Please show the JavaScript code (or whatever it might be) that causes something to be POSTed to the servlet, if it isn't a form. If you're looking for some built-in J2EE method to understand JSON object literals, there is none. – Jonathan Feinberg Oct 10 '09 at 19:25
  • 1
    Is your problem that you are trying to send a json object from the browser to the servlet, and you can't get the information on the servlet? – James Black Oct 11 '09 at 03:26
  • 1
    See also more popular http://stackoverflow.com/questions/3831680/httpservletrequest-get-post-data – Vadzim Apr 28 '14 at 15:12

8 Answers8

68

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());
}
Florent
  • 1,311
  • 1
  • 14
  • 15
user305224
  • 689
  • 5
  • 4
  • 6
    This method do convert json representation into a string, but I think it is probably not the best way to handle json data. – XY L Feb 04 '15 at 07:02
55

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

String requestData = request.getReader().lines().collect(Collectors.joining());
Dmitry Stolbov
  • 2,759
  • 2
  • 25
  • 23
41

make use of the jackson JSON processor

 ObjectMapper mapper = new ObjectMapper();
  Book book = mapper.readValue(request.getInputStream(),Book.class);
Luigi
  • 4,129
  • 6
  • 37
  • 57
Clyde D'Cruz
  • 411
  • 4
  • 2
31

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

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

MyBean myBean = gson.fromJson(reader, MyBean.class);
Edd
  • 8,402
  • 14
  • 47
  • 73
22

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

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

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

JSONObject json = new JSONObject(jsonString);
MyObject myObject = new Gson().fromJson(jsonString, MyObject.class);
xedo
  • 1,117
  • 3
  • 18
  • 34
  • IOUtils.toString is Deprecated I used "String jsonString = new String(ByteStreams.toByteArray(request.getInputStream()))" – Lelis718 Aug 10 '22 at 10:24
  • @Lelis718 this post is from 2015 and the question from 2009, stuff gets old – xedo Aug 23 '22 at 21:25
5

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-gson to 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.

arunjitsingh
  • 2,644
  • 2
  • 22
  • 16
0

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

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

//ABC class is a class whose strcuture matches to the data variable retrieved
John Conde
  • 217,595
  • 99
  • 455
  • 496
Ashok
  • 9
  • 1
0

Use following dependencies

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.10.1</version>
</dependency>

Version can be any latest version

We can process Json input with following line of code

BufferedReader reader = request.getReader();
Gson gson = new Gson();
MyBean myBean = gson.fromJson(reader, MyBean.class);