gpt4 book ai didi

java - 显示从 InputStream/InputReader 流式传输的数据,同时保留空白

转载 作者:行者123 更新时间:2023-12-02 01:39:22 24 4
gpt4 key购买 nike

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

产生 pretty-print 结果,因此具有良好的可读性:

 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] }

是否有可读的基于 Java 8 流的日志输出替代方法 2(通过 IOUtils)?

最佳答案

看起来您只想将请求中的每一行放在单独的行上。为此,您可以在每一行中附加一个换行符:

String body = request.getReader()
.lines()
.map(e -> e + System.lineSeparator())
//Or e.concat(...)
.collect(Collectors.joining());
log.info("Received Request {}",body);

关于java - 显示从 InputStream/InputReader 流式传输的数据,同时保留空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54659515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com