使用 Jackson 解析 HTTP 请求负载

Parsing HTTP request payload with Jackson

我希望将我的 HTTP 请求的有效负载转换为 JSON 字符串,我正在为此使用 Jackson 库。首先,我将其转换为 Map,然后转换为 JSON 字符串。

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

我遇到异常:

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[=11=]0(ServletInitialHandler.java:73)
2016-07-18 11:52:15,978 ERROR [stderr] (default task-2)  at io.undertow.servlet.handlers.ServletInitialHandler.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.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)

下一行:

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

打印:

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

我的令牌来了null

我错过了什么吗?从 HTTP 请求流中提取数据的正确方法是什么?

我是这样做的:

BufferedReader bufReader = new BufferedReader(new InputStreamReader
    (new URL(myUrlString).openStream()));

StringBuilder myStrBuilder = new StringBuilder();
    while((builderLine = bufReader.readLine()) != null) {
        myStrBuilder.append(builderLine);
    }

JSONArray myJsonArry = new JSONArray(myStrBuilder.toString()); 
List<Object> myArrayList = new ArrayList<Object>(toList(myJsonArry));

Collection<Map<String,String>> mapsCol = new HashSet<Map<String,String>>(); 

for (int i=0; i < myArrayList.size(); i++) {
    mapsCol.add((HashMap<String, String>)myArrayList.get(i));
}

以下应该可以解决问题:

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