Siddharth Trikha Siddharth Trikha - 4 months ago 31
Java Question

Parsing HTTP request payload with Jackson

I want the payload of my HTTP request to be converted into a JSON string and I am using Jackson library for it. First I convert it to a

Map
and then to a JSON string.

try {

ObjectMapper mapper = new ObjectMapper();
JsonParser jsonParser = mapper.getFactory().createParser(httpRequest.getInputStream());

System.out.println("JSON PARSER: " + jsonParser +
" TOKEN: " + jsonParser.getCurrentToken());

Map<String, Object> jsonMap = mapper.readValue(jsonParser, Map.class);

} catch (IOException e) {
e.printStackTrace();
}


I get exception:

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input ERROR [stderr] (default task-2) at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@4b68bbaa; line: 1, column: 1]
ERROR [stderr] (default task-2) at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
ERROR [stderr] (default task-2) at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3029)
ERROR [stderr] (default task-2) at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2944)
ERROR [stderr] (default task-2) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1587)
ERROR [stderr] (default task-2)
ERROR [stderr] (default task-2) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2016-07-18 11:52:15,973 ERROR [stderr] (default task-2) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
2016-07-18 11:52:15,973 ERROR [stderr] (default task-2) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2016-07-18 11:52:15,973 ERROR [stderr] (default task-2) at java.lang.reflect.Method.invoke(Method.java:606)
2016-07-18 11:52:15,973 ERROR [stderr] (default task-2) at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)
2016-07-18 11:52:15,973 ERROR [stderr] (default task-2) at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:280)
2016-07-18 11:52:15,974 ERROR [stderr] (default task-2) at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:234)
2016-07-18 11:52:15,974 ERROR [stderr] (default task-2) at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:221)
2016-07-18 11:52:15,974 ERROR [stderr] (default task-2) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
2016-07-18 11:52:15,974 ERROR [stderr] (default task-2) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
2016-07-18 11:52:15,974 ERROR [stderr] (default task-2) at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
2016-07-18 11:52:15,974 ERROR [stderr] (default task-2) at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
2016-07-18 11:52:15,975 ERROR [stderr] (default task-2) at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
2016-07-18 11:52:15,975 ERROR [stderr] (default task-2) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
2016-07-18 11:52:15,975 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
2016-07-18 11:52:15,975 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
2016-07-18 11:52:15,975 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
2016-07-18 11:52:15,975 ERROR [stderr] (default task-2) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
2016-07-18 11:52:15,975 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
2016-07-18 11:52:15,976 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
2016-07-18 11:52:15,976 ERROR [stderr] (default task-2) at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52)
2016-07-18 11:52:15,976 ERROR [stderr] (default task-2) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
2016-07-18 11:52:15,976 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
2016-07-18 11:52:15,976 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
2016-07-18 11:52:15,976 ERROR [stderr] (default task-2) at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
2016-07-18 11:52:15,976 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
2016-07-18 11:52:15,977 ERROR [stderr] (default task-2) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
2016-07-18 11:52:15,977 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
2016-07-18 11:52:15,977 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
2016-07-18 11:52:15,977 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
2016-07-18 11:52:15,977 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
2016-07-18 11:52:15,977 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
2016-07-18 11:52:15,978 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
2016-07-18 11:52:15,978 ERROR [stderr] (default task-2) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:168)
2016-07-18 11:52:15,978 ERROR [stderr] (default task-2) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:687)
2016-07-18 11:52:15,978 ERROR [stderr] (default task-2) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
2016-07-18 11:52:15,978 ERROR [stderr] (default task-2) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
2016-07-18 11:52:15,978 ERROR [stderr] (default task-2) at java.lang.Thread.run(Thread.java:724)





The following line:

System.out.println("JSON PARSER: " + jsonParser +
" TOKEN: " + jsonParser.getCurrentToken());


Prints:

JSON PARSER: com.fasterxml.jackson.core.json.UTF8StreamJsonParser@6634789d TOKEN: null


My token is coming
null
.

Am I missing something? What is the corrcet way to extract data from HTTP request stream?

Answer

The following should do the trick:

// Read the request payload into a String
StringBuilder buffer = new StringBuilder();
BufferedReader reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
    buffer.append(line);
}
String data = buffer.toString();

// If the String is not empty, parses the payload into a map
Map<String, Object> jsonMap = null;
if (!data.isEmpty()) {
    ObjectMapper mapper = new ObjectMapper();
    jsonMap = mapper.readValue(data, Map.class);
}