W3docs

HttpServletRequest get JSON POST data

To get JSON POST data from an HttpServletRequest object in Java, you can use the getReader method of the ServletRequest interface to read the request body as a BufferedReader and then use the readLine method to read the data as a string.

To get JSON POST data from an HttpServletRequest object in Java, you can use the getReader method of the ServletRequest interface to read the request body as a BufferedReader. Note that getReader() can only be invoked once per request. You can then use the readLine method to read the data as a string.

Here is an example of how you can do this:


import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;

public class Main {
  public static String readJsonPostData(HttpServletRequest request) throws IOException {
    StringBuilder sb = new StringBuilder();
    try (BufferedReader reader = request.getReader()) {
      String line;
      while ((line = reader.readLine()) != null) {
        sb.append(line);
      }
    }
    return sb.toString();
  }
}

Once you have the JSON POST data as a string, you can use a JSON library such as Jackson to parse it and access the individual fields.

I hope this helps! Let me know if you have any other questions.