在Servlet中捕获HTTP请求的正文时,根据所使用的方法,我会看到不同的行为。
方法1:(Java 8流)
String body = request.getReader().lines().collect(Collectors.joining());
log.info("Received Request {}",body);
将所有日志压缩成一行,因此难以阅读:
[java] 02-12-2019 22:14:43 [qtp2056234595-330] INFO domain.lola.user.utils.actionsongoogle.AoGBotService [handlePOST:98] - Received AoG Request { "responseId": "a3da42ef-5728-4d85-bb69-0643f5785804", "queryResult": { "queryText": "GOOGLE_ASSISTANT_WELCOME", "parameters": { "any": "" }, "allRequiredParamsPresent": true, "fulfillmentMessages": [{ "text": { "text": [""] } }], "outputContexts": [{ "name": "projects/speechbank-e8a15/agent/sessions/ABwppHGy_13Y1OZAVltA6Hj5qTsReoZ8MQrajc0NjBmzNd2Q8Hl7TxIs8x4CFKDlAAqhLGCO82hcwulZt9ZxXw/contexts/google_assistant_welcome", "parameters": { "any.original": "", "any": "" } }, { "name": "projects/speechbank-e8a15/agent/sessions/ABwppHGy_13Y1OZAVltA6Hj5qTsReoZ8MQrajc0NjBmzNd2Q8Hl7TxIs8x4CFKDlAAqhLGCO82hcwulZt9ZxXw/contexts/actions_capability_screen_output", "parameters": { "any.original": "", "any": "" } }, { "name": "projects/speechbank-e8a15/agent/sessions/ABwppHGy_13Y1OZAVltA6Hj5qTsReoZ8MQrajc0NjBmzNd2Q8Hl7TxIs8x4CFKDlAAqhLGCO82hcwulZt9ZxXw/contexts/actions_capability_audio_output", "parameters": { "any.original": "", "any": "" } }, { "name": "projects/speechbank-e8a15/agent/sessions/ABwppHGy_13Y1OZAVltA6Hj5qTsReoZ8MQrajc0NjBmzNd2Q8Hl7TxIs8x4CFKDlAAqhLGCO82hcwulZt9ZxXw/contexts/google_assistant_input_type_keyboard", "parameters": { "any.original": "", "any": "" } }, { "name": "projects/speechbank-e8a15/agent/sessions/ABwppHGy_13Y1OZAVltA6Hj5qTsReoZ8MQrajc0NjBmzNd2Q8Hl7TxIs8x4CFKDlAAqhLGCO82hcwulZt9ZxXw/contexts/actions_capability_web_browser", "parameters": { "any.original": "", "any": "" } }, { "name": "projects/speechbank-e8a15/agent/sessions/ABwppHGy_13Y1OZAVltA6Hj5qTsReoZ8MQrajc0NjBmzNd2Q8Hl7TxIs8x4CFKDlAAqhLGCO82hcwulZt9ZxXw/contexts/actions_capability_media_response_audio", "parameters": { "any.original": "", "any": "" } }], "intent": { "name": "projects/speechbank-e8a15/agent/intents/f645f492-f6dc-4e7e-8da6-45711c654ad0", "displayName": "RawText" }, "intentDetectionConfidence": 1.0, "languageCode": "en-us" }, "originalDetectIntentRequest": { "source": "google", "version": "2", "payload": { "isInSandbox": true, "surface": { "capabilities": [{ "name": "actions.capability.WEB_BROWSER" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" }] }, "requestType": "SIMULATOR", "inputs": [{ "rawInputs": [{ "query": "Talk to speech bank", "inputType": "KEYBOARD" }], "intent": "actions.intent.MAIN" }], "user": { "userStorage": "{\"data\":{}}", "lastSeen": "2019-02-12T22:01:13Z", "locale": "en-US", "userId": "ABwppHFQHUBr0RrWA_OuL-kK2sxTPUvQtL3D-x2Ydr-7uxLt9zzEFzJrGB-X96d9XY8k9XTJj-RUg9WpzGB9jg" }, "conversation": { "conversationId": "ABwppHGy_13Y1OZAVltA6Hj5qTsReoZ8MQrajc0NjBmzNd2Q8Hl7TxIs8x4CFKDlAAqhLGCO82hcwulZt9ZxXw", "type": "NEW" } } }, "session": "projects/speechbank-e8a15/agent/sessions/ABwppHGy_13Y1OZAVltA6Hj5qTsReoZ8MQrajc0NjBmzNd2Q8Hl7TxIs8x4CFKDlAAqhLGCO82hcwulZt9ZxXw"}
方法2:(Apache Commons IOUtils)
String rawRequest = IOUtils.toString(IOUtils.toByteArray(request.getInputStream()),UTF_8);
log.info("Received request: {}", rawRequest);
产生漂亮的印刷品,因此可读性强:
domain.lola.user.utils.actionsongoogle.AoGBotService [handlePOST:101] - Received request: {
[java] "responseId": "f89b8b29-7e4d-4eb6-bb15-f693d8735626",
[java] at domain.lola.user.utils.KaiBotService.handle(KaiBotService.java:365)
[java] at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
[java] "queryResult": {
[java] "queryText": "GOOGLE_ASSISTANT_WELCOME",
[java] "parameters": {
[java] "any": ""
[java] },
[java] at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
[java] "allRequiredParamsPresent": true,
[java] "fulfillmentMessages": [{
[java] "text": {
[java] "text": [""]
[java] }
[java] }],
[java] "outputContexts": [{
[java] at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
[java] at org.eclipse.jetty.server.Server.handle(Server.java:369)
[java] at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:486)
[java] "name": "projects/speechbank-e8a15/agent/sessions/ABwppHFnxMijkuLestJ-UC840GbwybjcxwZ7csve5h2Ee2qiqEUpDqY1c2FyqHancGHTdWEpuXtqL-zyafrTBA/contexts/google_assistant_welcome",
[java] "parameters": {
[java] "any.original": "",
[java] "any": ""
[java] }
[java] at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:944)
[java] at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1005)
[java] at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
[java] at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
[java] at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
[java] at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667)
[java] }, {
[java] at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
[java] at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
[java] "name": "projects/speechbank-e8a15/agent/sessions/ABwppHFnxMijkuLestJ-UC840GbwybjcxwZ7csve5h2Ee2qiqEUpDqY1c2FyqHancGHTdWEpuXtqL-zyafrTBA/contexts/actions_capability_screen_output",
[java] "parameters": {
[java] "any.original": "",
[java] at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
[java] at java.base/java.lang.Thread.run(Thread.java:844)
[java] "any": ""
[java] }
[java] }, {
[java] "name": "projects/speechbank-e8a15/agent/sessions/ABwppHFnxMijkuLestJ-UC840GbwybjcxwZ7csve5h2Ee2qiqEUpDqY1c2FyqHancGHTdWEpuXtqL-zyafrTBA/contexts/actions_capability_audio_output",
[java] "parameters": {
[java] "any.original": "",
[java] "any": ""
[java] }
[java] }, {
[java] "name": "projects/speechbank-e8a15/agent/sessions/ABwppHFnxMijkuLestJ-UC840GbwybjcxwZ7csve5h2Ee2qiqEUpDqY1c2FyqHancGHTdWEpuXtqL-zyafrTBA/contexts/google_assistant_input_type_keyboard",
[java] "parameters": {
[java] "any.original": "",
[java] "any": ""
[java] }
[java] }, {
[java] Caused by: java.lang.Exception: Intent handler not found - RawText
[java] "name": "projects/speechbank-e8a15/agent/sessions/ABwppHFnxMijkuLestJ-UC840GbwybjcxwZ7csve5h2Ee2qiqEUpDqY1c2FyqHancGHTdWEpuXtqL-zyafrTBA/contexts/actions_capability_web_browser",
[java] at com.google.actions.api.DefaultApp.handleError(DefaultApp.kt:103)
[java] "parameters": {
[java] "any.original": "",
[java] "any": ""
[java] }
[java] }, {
[java] at com.google.actions.api.DefaultApp.handleError(DefaultApp.kt:98)
[java] at com.google.actions.api.DefaultApp.handleRequest(DefaultApp.kt:62)
[java] "name": "projects/speechbank-e8a15/agent/sessions/ABwppHFnxMijkuLestJ-UC840GbwybjcxwZ7csve5h2Ee2qiqEUpDqY1c2FyqHancGHTdWEpuXtqL-zyafrTBA/contexts/actions_capability_media_response_audio",
[java] "parameters": {
[java] "any.original": "",
[java] "any": ""
[java] }
[java] }],
[java] "intent": {
[java] ... 17 more
[java] "name": "projects/speechbank-e8a15/agent/intents/f645f492-f6dc-4e7e-8da6-45711c654ad0",
[java] "displayName": "RawText"
[java] },
[java] "intentDetectionConfidence": 1.0,
[java] "languageCode": "en-us"
[java] },
[java] "originalDetectIntentRequest": {
[java] "source": "google",
[java] "version": "2",
[java] "payload": {
[java] "isInSandbox": true,
[java] "surface": {
[java] "capabilities": [{
[java] "name": "actions.capability.SCREEN_OUTPUT"
[java] }, {
[java] "name": "actions.capability.MEDIA_RESPONSE_AUDIO"
[java] }, {
[java] "name": "actions.capability.WEB_BROWSER"
[java] }, {
[java] "name": "actions.capability.AUDIO_OUTPUT"
[java] }]
[java] },
[java] "requestType": "SIMULATOR",
[java] "inputs": [{
[java] "rawInputs": [{
[java] "query": "Talk to speech bank",
[java] "inputType": "KEYBOARD"
[java] }],
[java] "intent": "actions.intent.MAIN"
[java] }],
[java] "user": {
[java] "userStorage": "{\"data\":{}}",
[java] "lastSeen": "2019-02-12T21:39:58Z",
[java] "locale": "en-US",
[java] "userId": "ABwppHFQHUBr0RrWA_OuL-kK2sxTPUvQtL3D-x2Ydr-7uxLt9zzEFzJrGB-X96d9XY8k9XTJj-RUg9WpzGB9jg"
[java] },
[java] "conversation": {
[java] "conversationId": "ABwppHFnxMijkuLestJ-UC840GbwybjcxwZ7csve5h2Ee2qiqEUpDqY1c2FyqHancGHTdWEpuXtqL-zyafrTBA",
[java] "type": "NEW"
[java] }
[java] }
[java] },
[java] "session": "projects/speechbank-e8a15/agent/sessions/ABwppHFnxMijkuLestJ-UC840GbwybjcxwZ7csve5h2Ee2qiqEUpDqY1c2FyqHancGHTdWEpuXtqL-zyafrTBA"
[java] }
是否存在方法2的可替代的,基于Java 8流的可读日志输出(通过IOUtils)?
答案 0 :(得分:1)
您似乎只想将请求中的每一行都放在单独的行上。为此,您可以在每行后面添加换行符:
String body = request.getReader()
.lines()
.map(e -> e + System.lineSeparator())
//Or e.concat(...)
.collect(Collectors.joining());
log.info("Received Request {}",body);
答案 1 :(得分:1)
@GBlodgett建议的代码未编译。
以下表达式对我有用:
String rawRequest = request.getReader()
.lines()
.map(e -> e.concat(System.lineSeparator()))
.collect(Collectors.joining());
log.info("Received AoG Request {}",rawRequest);
或更有效,基于@Holger的建议:
String rawRequest = request.getReader()
.lines()
//.map(e -> e.concat(System.lineSeparator()))
.collect(Collectors.joining(System.lineSeparator()));
log.info("Received AoG Request {}",rawRequest);
我将这两个选择之间的效率增益的测量结果留给读者。
这两种选择都不需要任何外部依赖关系,在我的情况下,它们优先于 IOUtils
选项。