- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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/
我正在尝试使用 JAXB 从单个输入流中读取多个 xml 文档。由于 JAXB 本身不支持此功能,因此我创建了自己的 XmlReader。但是,这迫使我在这个单一输入流上使用多个 XMLEventRe
我有一个使用 ServerSocket/Socket 的 java 客户端/服务器程序。 在我的客户端中,我有一个线程(在 ReceiverThread 中调用),它始终等待来自服务器的传入消息。所以
在 servlet 中捕获 HTTP 请求正文时,我发现根据我使用的方法不同会有不同的行为。 方法 1:(Java 8 流) String body = request.getReader().lin
我对 JSON 有疑问。我通过互联网搜索,但没有找到适合我的解决方案。 为了更好地工作,我将服务器的答案 (JSON) 保存在 *.json 文件中。JSON 看起来像这样(简短版本): {"data
我想编写通用代码来解析用户输入并将其与类数组进行比较,然后相应地转换输入。即: Object[] paramTypes = { String.class, String.class, Integer.
我是一名优秀的程序员,十分优秀!