Retrieving JSON Object Literal from HttpServletRequest

JsonPostServlets

Json Problem Overview


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.

Json Solutions


Solution 1 - Json

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());
}

Solution 2 - Json

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

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

Solution 3 - Json

make use of the jackson JSON processor

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

Solution 4 - Json

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);

Solution 5 - Json

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);

Solution 6 - Json

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 http://code.google.com/p/google-gson">google-gson</a> 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.

Solution 7 - Json

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarthMaulView Question on Stackoverflow
Solution 1 - Jsonuser305224View Answer on Stackoverflow
Solution 2 - JsonDmitry StolbovView Answer on Stackoverflow
Solution 3 - JsonClyde D'CruzView Answer on Stackoverflow
Solution 4 - JsonEddView Answer on Stackoverflow
Solution 5 - JsonxedoView Answer on Stackoverflow
Solution 6 - JsonarunjitsinghView Answer on Stackoverflow
Solution 7 - JsonAshokView Answer on Stackoverflow