{"id":10021079,"date":"2025-11-16T19:02:07","date_gmt":"2025-11-16T11:02:07","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021079"},"modified":"2025-11-16T19:02:07","modified_gmt":"2025-11-16T11:02:07","slug":"sseemitter%e8%bf%94%e5%9b%9edata%e8%a2%ab%e5%8f%8c%e5%bc%95%e5%8f%b7%e5%8c%85%e8%a3%b9%e7%9a%84%e9%97%ae%e9%a2%98%e6%8e%92%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021079","title":{"rendered":"SseEmitter\u8fd4\u56dedata\u88ab\u53cc\u5f15\u53f7\u5305\u88f9\u7684\u95ee\u9898\u6392\u67e5"},"content":{"rendered":"<h2>\u4e00\u3001\u80cc\u666f<\/h2>\n<p>\u6700\u8fd1\u505a\u63a5\u53e3\u7684\u6027\u80fd\u6539\u9020\uff0c\u5927\u6982\u80cc\u666f\u5982\u4e0b:<br \/>\u65e7: <br \/>  1.\u524d\u7aef\u6bcf\u79d2\u8f6e\u8be2\u540e\u7aef\u63a5\u53e3\uff0c\u63a5\u53e3\u8fd4\u56de\u6570\u636e\u72b6\u6001\uff0c\u524d\u7aef\u7528\u72b6\u6001\u505a\u5224\u65ad\uff0c\u53d8\u66f4\u9875\u9762\u4ea4\u4e92\u3002<br \/>  2.\u524d\u7aef\u56fa\u5b9a\u8c03\u7528\u540e\u7aef\u63a5\u53e3\uff0c\u63a5\u53e3\u963b\u585e100\u79d2\uff0c\u7b49\u5f85\u540e\u7aef\u968f\u65f6\u8fd4\u56de\u7ed3\u679c\uff0c100\u79d2\u5230\u8fbe\u540e\u65e0\u7ed3\u679c\uff0c\u76f4\u63a5\u5931\u8d25\u3002<\/p>\n<p>\u65b0: <br \/>  \u6539\u4e3a<code>ServerSentEvent<\/code>\u4ee5<code>text-event-stream<\/code>\u56fa\u5b9a\u65f6\u95f4\u7a97\u53e3\u7531\u540e\u7aef\u8fd4\u56de\u5904\u7406\u8fdb\u5ea6\u3002<\/p>\n<h2>\u4e8c\u3001\u7b80\u5355\u5bf9\u6bd4<\/h2>\n<p>1.\u540e\u7aef\u670d\u52a1\u538b\u529b\u5927\u3002\u5c24\u5176\u5728\u7528\u6237\u91cf\u5927\uff0c\u5e76\u53d1\u91cf\u5927\uff0c\u53c8\u662f\u7cfb\u7edf\u6838\u5fc3\u4e1a\u52a1\u65f6\uff0c\u4e0d\u597d\u628a\u63a7\u3002<br \/>2.\u963b\u585e<code>Servlet<\/code>\u7ebf\u7a0b, \u5f53\u524d\u4e1a\u52a1\u540c\u65f6\u8bbf\u95ee\u91cf\u5927\u65f6\uff0c\u5f71\u54cd\u5176\u4ed6\u4e1a\u52a1\u8bf7\u6c42\uff1b\u6700\u5927\u7b49\u5f85\u65f6\u95f4\u4e0d\u597d\u628a\u63a7\uff0c\u4e14\u53d7HTTP\u54cd\u5e94\u8d85\u65f6\u65f6\u95f4\u5f71\u54cd\u3002<br \/>3.\u81ea\u7531\u7b80\u5355\uff0c\u4e14\u5b9e\u65f6\u6027\u6bd4\u8f83\u5f3a\uff0c\u53ea\u8981\u4e0d\u662f\u9ad8\u5e76\u53d1\u5165\u53e3\u8bf7\u6c42\u4e1a\u52a1\u5c31\u80fd\u4f7f\u7528\u3002\u4e0d\u5360\u7528<code>Servlet<\/code>\u7ebf\u7a0b\u3002<\/p>\n<h2>\u4e09\u3001\u95ee\u9898\u73b0\u8c61<\/h2>\n<h3>1.\u4e3b\u4ee3\u7801\u5c55\u793a<\/h3>\n<blockquote><p>\u4ee5\u4e0b\u4ee3\u7801\u5747\u4e3a\u7cbe\u7b80\u540e\u7684\u6700\u5c0f\u95ee\u9898\u590d\u73b0demo<\/p><\/blockquote>\n<ul>\n<li>SSE\u4e3b\u63a5\u53e3<br \/><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"535\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3JS\" alt=\"PixPin_2025-11-16_11-57-06.png\" title=\"PixPin_2025-11-16_11-57-06.png\"><\/li>\n<\/ul>\n<h3>2.\u95ee\u9898\u8868\u73b0<\/h3>\n<p>\u901a\u8fc7<code>curl<\/code>\u52a0<code>-N<\/code>\u53c2\u6570\u8c03\u7528\u540e\uff0c\u53d1\u73b0\u8fd4\u56de\u7684<code>data<\/code>\u90fd\u88ab\u53cc\u5f15\u53f7\u5305\u88f9\uff0c\u5e76\u4e14\u524d\u7aef\u7684<code>EventSource.onmessage<\/code>\u65e0\u6cd5\u56de\u8c03\u5230\uff08\u603b\u662f\u89e6\u53d1<code>onerror<\/code>\u56de\u8c03\uff09<br \/><img decoding=\"async\" width=\"723\" height=\"609\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3JT\" alt=\"PixPin_2025-11-16_11-59-00.png\" title=\"PixPin_2025-11-16_11-59-00.png\" loading=\"lazy\"><\/p>\n<h2>\u56db.\u95ee\u9898\u601d\u8003<\/h2>\n<p>\u53c2\u8003\u5e76\u9605\u8bfb\u4ee5\u4e0b\u6587\u6863<br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=fYdgVBoHTdD6W0hrGQImBg%3D%3D.Z%2BNrIZpDTj8klqEflbBhGQCk9A0KVlz94DxvaV6OwhdfpC9tkYHhPFZTXjuOaZ5o%2B%2FIoVZaW3srYoBm7f4o89ZjCRIuXqGabBNmgXvI45VGYjA0kQIc%2F%2B0yRma1d5Ytl\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u524d\u7aefMDN ServerSentEvent\u6587\u6863<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=JIPzdFAsmX5SgTLpno2Tuw%3D%3D.04h3lW3G4e2cMDPmxd2wvfFjdWqASfodCeWrxvT%2BqDvHCOHi4UNZa9SDuZIG9daDPBh5YTi%2BYz62d9uiPhED%2B6qO735fYCo12wlQkqtIc4%2ByqFqK987%2FVRMdQitqS4wXeVuhJTrb1JrtjWpIvgFDe%2BkC%2Btu8wx5bXZQ%2Bi8oB7FGrLS8nj3EweyGbhAG5yBrA8EAZghRv77U9OFJAR5%2B8srVeOUzT6CpHpLYP27NBEc4%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Spring ServerSentEvent\u6587\u6863<\/a><\/p>\n<p>\u5728\u524d\u7aef\u4fa7\u6d4f\u89c8\u5668\u770b\u5230\u5f00\u53d1\u8005\u5de5\u5177\u5185<code>EventStream<\/code>\u6807\u7b7e\u9875\u663e\u793a\u7a7a\u767d\uff0c \u610f\u8bc6\u5230\u786e\u5b9e\u662f\u540e\u7aef\u81ea\u8eab\u95ee\u9898\uff0c\u89c2\u5bdf\u6570\u636e\u7ed3\u6784\u540e\u53d1\u73b0\uff0c\u50cf\u662f\u8fd4\u56de\u4e86JSON\u7ed3\u6784\u7684<code>String<\/code>\u5b57\u7b26\u4e32\uff0c\u53ea\u6709JSON\u7ed3\u6784\u4e0b<code>String<\/code>\u4e24\u4fa7\u624d\u4f1a\u6709\u5f15\u53f7\uff0c\u63a5\u7740\u7acb\u523b\u7ffb\u6e90\u7801\u9a8c\u8bc1\u3002<\/p>\n<p>\u4ece\u6e90\u7801<code>org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler.HttpMessageConvertingHandler#sendInternal<\/code>\u4e2d\u627e\u5230\u7ebf\u7d22\uff0c\u53d1\u73b0<code>Spring<\/code>\u5728\u5904\u7406<code>text-event-stream<\/code>\u54cd\u5e94\u65f6\uff0c\u4ecd\u7136\u662f\u4eceServlet Web\u7684\u5168\u5c40<code>HttpMessageConverter<\/code>\u67e5\u627e\uff0c\u4f46\u8fd9\u91cc\u7684<code>mediaType<\/code>\u503c\u5230\u5e95\u662f\u54ea\u4e2a\u503c\u5462\uff0c\u5b58\u5728\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5<\/p>\n<ul>\n<li><code>Controller<\/code>\u65b9\u6cd5\u6574\u4f53\u7684<code>MediaType<\/code>\uff0c\u8fd9\u4e2a\u503c\u56fa\u5b9a\u662f<code>text\/event-stream<\/code><\/li>\n<li><code>SseEmitter#send(java.lang.Object, org.springframework.http.MediaType)<\/code>\u65b9\u6cd5\u6574\u4f53\u7684<code>MediaType<\/code>\uff0c\u8fd9\u4e2a\u503c\u56fa\u5b9a\u662f<code>text\/plain<\/code><\/li>\n<li><code>SseEmitter#send(java.lang.Object, org.springframework.http.MediaType)<\/code>\u65b9\u6cd5\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u5165\u7684<code>MediaType<\/code>\uff0c\u8fd9\u4e2a\u503c\u52a8\u6001\u7684\uff0c\u6211\u8fd9\u91cc\u662f<code>application\/json;charset=UTF-8<\/code><\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u5927\u6982\u7387\u662f\u7b2c\u4e8c\u79cd\u5bfc\u81f4\u7684\u3002<br \/><img decoding=\"async\" width=\"723\" height=\"422\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3JX\" alt=\"PixPin_2025-11-16_12-09-33.png\" title=\"PixPin_2025-11-16_12-09-33.png\" loading=\"lazy\"><\/p>\n<h2>\u4e94\u3001\u5f97\u5230\u7ed3\u8bba<\/h2>\n<p>\u7531\u4e8e\u5728K8s\u5185\u7f51\u90e8\u7f72\uff0c\u672c\u673a\u65e0\u6cd5\u542f\u52a8\uff0c\u5173\u8054\u7684\u4e2d\u95f4\u4ef6\u548c\u9644\u52a0Bean\u592a\u591a\u3002\u5230\u8fd9\u91cc\u4e3a\u6b62\uff0c\u5176\u5b9e\u95ee\u9898\u6e05\u6670\u4e86\uff0c\u7ed3\u5408\u5bf9Spring\u6e90\u7801\u7684\u7406\u89e3\uff0c<strong>\u9879\u76ee\u4e2d\u7684\u5168\u5c40<code>HttpMessageConverter<\/code>\u914d\u7f6e\u80af\u5b9a\u6709\u95ee\u9898<\/strong>\uff0c\u6211\u4eec\u516c\u53f8\u524d\u8eab\u6709\u5f88\u591a\u8ba4\u4e3a\u81ea\u5df1\u5f88\u5389\u5bb3\u7684\u4eba\uff0c\u90fd\u662f\u4ece\u963f\u91cc\u8f6c\u8fc7\u6765\u7684\uff0c\u9177\u7231<code>fastjson<\/code>\uff0c\u9879\u76ee\u57fa\u5efa\u65f6\uff0c\u80af\u5b9a\u5e72\u6389\u4e86\u6240\u6709\u7684<code>HttpMessageConverter<\/code>\uff0c\u7edf\u4e00\u6362\u6210\u4e86<code>fastjson<\/code>\uff0c\u63a5\u7740\u627e\u8fd9\u4e2a\u4ee3\u7801<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"450\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3J1\" alt=\"PixPin_2025-11-16_12-25-22.png\" title=\"PixPin_2025-11-16_12-25-22.png\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u662f2021\u5e74\uff0c\u524d\u4eba\u7559\u4e0b\u6765\u7684\u5b9d\u85cf\uff0c\u53ea\u80fd\u8bf4\u8fd9\u4e2a\u4eba\u590d\u5236\u7684\u65f6\u5019\u53ef\u80fd\u6ca1\u8fc7\u8111<\/p>\n<ul>\n<li>\u7ea2\u8272\u6846\u51fa\u6765\u7684\u5c31\u662f\u6839\u56e0<\/li>\n<li>\u7eff\u8272\u6846\u51fa\u6765\u7684\u5c31\u662f\u5b8c\u5168\u65e0\u8111\uff0c\u4e8c\u8fdb\u5236\u6587\u6863\uff0c\u56fe\u7247\uff0c\u4e5f\u8981Json\u5417\uff1f<\/li>\n<\/ul>\n<h3>1.\u770b\u770b\u4e0a\u6e38Spring\u7684\u5904\u7406<\/h3>\n<blockquote><p>\u968f\u5373\u95ee\u9898\u6765\u4e86\uff0cSpring\u9ed8\u8ba4\u4f1a\u6709\u4e00\u5927\u5806\u7684<code>HttpMessageConverter<\/code>\u81ea\u52a8\u914d\u7f6e\uff0c\u5f53\u524d\u8fd9\u4e2a\u662f<code>add<\/code>\u5230<code>List&lt;HttpMessageConverter&lt;?&gt;&gt; converters<\/code>\u7684\u7b2c\u4e00\u4f4d\u5417\uff1f\u9ed8\u8ba4\u7684\u8fd8\u5728\u4e0d\u5728\uff1f<\/p><\/blockquote>\n<p>\u6e90\u7801\u5982\u4e0b: <code>org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#getMessageConverters<\/code><br \/><img decoding=\"async\" width=\"723\" height=\"451\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3J2\" alt=\"PixPin_2025-11-16_12-29-52.png\" title=\"PixPin_2025-11-16_12-29-52.png\" loading=\"lazy\"><\/p>\n<p><code>org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#addDefaultHttpMessageConverters<\/code>\u5982\u4e0b<br \/>\u5982\u56fe\u5c31\u662f<code>Spring<\/code>\u7684\u89e3\u6cd5<br \/><img decoding=\"async\" width=\"723\" height=\"540\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3J3\" alt=\"PixPin_2025-11-16_12-31-54.png\" title=\"PixPin_2025-11-16_12-31-54.png\" loading=\"lazy\"><\/p>\n<h2>\u516d\u3001\u5708\u5b9a\u5f71\u54cd<\/h2>\n<p>\u90a3\u4e48\u65b0\u7684\u95ee\u9898\u6765\u4e86\uff0c\u5982\u679c\u8fd9\u4e2a\u9879\u76ee\u4e2d\u6709\u5176\u4ed6\u76f4\u63a5\u8fd4\u56de<code>String<\/code>\u7684\u63a5\u53e3\uff0c\u5c82\u4e0d\u662f\u610f\u5473\u7740\u5168\u90e8\u88ab\u5305\u4e86\u4e00\u5c42\u53cc\u5f15\u53f7\uff1f<\/p>\n<p>\u6070\u597d\u60f3\u8d77\u4e4b\u524d\u6709\u5176\u4ed6\u4eba\u4e0d\u542c\u529d\uff0c\u4e0d\u4f7f\u7528<code>Spring Actuator<\/code>\u7684\u5065\u5eb7\u68c0\u67e5\uff0c\u975e\u8981\u624b\u6413\uff0c\u8fd4\u56de\u7684\u6070\u597d\u662f<code>String<\/code>,\u7ebf\u4e0a\u9a8c\u8bc1\u4e00\u4e0b<br \/><img decoding=\"async\" width=\"723\" height=\"227\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3J5\" alt=\"PixPin_2025-11-16_12-37-48.png\" title=\"PixPin_2025-11-16_12-37-48.png\" loading=\"lazy\"><\/p>\n<p>\u90a3\u5c31\u662f\u5f71\u54cd\u5230\u4e86\u6240\u6709\u76f4\u63a5\u8fd4\u56de<code>String<\/code>\u7684\u63a5\u53e3\u3002<\/p>\n<h2>\u4e03\u3001\u95ee\u9898\u590d\u73b0<\/h2>\n<blockquote><p>\u672c\u5730\u4e0d\u80fd\u542f\u52a8\uff0c\u6700\u5c0fdemo\uff0cdebug\u4e00\u4e0b<\/p><\/blockquote>\n<p>debug\u542f\u52a8\u540e\u53d1\u73b0\u786e\u5b9e\u662f\u8fd9\u91cc\u5f71\u54cd\u7684\uff0c\u8fd9\u91cc\u4f1a\u8d70\u4e24\u6b21<br \/>\u4e00\u6b21\u524d\u9762\u7684<code>text\/plain<\/code><br \/><img decoding=\"async\" width=\"723\" height=\"301\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3JZ\" alt=\"PixPin_2025-11-16_12-17-41.png\" title=\"PixPin_2025-11-16_12-17-41.png\" loading=\"lazy\"><br \/>\u5b9e\u9645\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684<code>MediaType<\/code>\uff0c\u503c<code>application\/json;charset=UTF-8<\/code><br \/><img decoding=\"async\" width=\"723\" height=\"297\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3J0\" alt=\"PixPin_2025-11-16_12-18-09.png\" title=\"PixPin_2025-11-16_12-18-09.png\" loading=\"lazy\"><\/p>\n<p>\u770b\u770b<code>HttpMessageConverter<\/code>\u662f\u4e0d\u662f\u9519\u9009\u4e86<br \/><img decoding=\"async\" width=\"723\" height=\"435\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3J7\" alt=\"PixPin_2025-11-16_12-43-27.png\" title=\"PixPin_2025-11-16_12-43-27.png\" loading=\"lazy\"><\/p>\n<p><strong>\u6839\u56e0\u786e\u5b9a: <code>text\/plain<\/code>\u9519\u8bef\u914d\u7f6e\u4e86\u4f7f\u7528<code>FastJsonHttpMessageConverter<\/code>,\u5bfc\u81f4\u8fd4\u56de\u7684\u4e0d\u662f\u5b57\u7b26\u4e32\uff0c\u800c\u662fjson\u5b57\u7b26\u4e32<\/strong><\/p>\n<h2>\u516b\u3001\u95ee\u9898\u4fee\u590d<\/h2>\n<p>\u4fee\u590d\u5c31\u975e\u5e38\u7b80\u5355\u4e86\uff0c\u5220\u9664\u4e0d\u5408\u7406\u7684<code>MediaType<\/code>\u914d\u7f6e\uff0c\u6700\u524d\u9762\u8ffd\u52a0<code>StringHttpMessageConverter<\/code>\u5373\u53ef<br \/><img decoding=\"async\" width=\"723\" height=\"304\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3J9\" alt=\"PixPin_2025-11-16_12-50-20.png\" title=\"PixPin_2025-11-16_12-50-20.png\" loading=\"lazy\"><\/p>\n<blockquote><p>\u5f53\u7136\u4e3a\u4e86\u7f29\u5c0f\u5f71\u54cd\u8303\u56f4\uff0c\u53ea\u5220\u9664<code>MediaType.TEXT_PLAIN<\/code>\u914d\u7f6e\u5373\u53ef<\/p><\/blockquote>\n<p>\u81f3\u6b64\uff0c\u95ee\u9898\u4fee\u590d\u3002<br \/><img decoding=\"async\" width=\"723\" height=\"142\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3Ka\" alt=\"PixPin_2025-11-16_12-52-46.png\" title=\"PixPin_2025-11-16_12-52-46.png\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047402597\">https:\/\/segmentfault.com\/a\/1190000047402597<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u80cc\u666f \u6700\u8fd1\u505a\u63a5\u53e3\u7684\u6027\u80fd\u6539\u9020\uff0c\u5927\u6982\u80cc\u666f\u5982\u4e0b:\u65e7: 1.\u524d\u7aef\u6bcf\u79d2\u8f6e\u8be2\u540e\u7aef\u63a5\u53e3\uff0c\u63a5\u53e3\u8fd4\u56de\u6570\u636e\u72b6\u6001\uff0c\u524d\u7aef\u7528\u72b6\u6001\u505a\u5224 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021079","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021079"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021079\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}