{"id":37222,"date":"2024-11-26T08:23:34","date_gmt":"2024-11-26T00:23:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37222\/"},"modified":"2024-11-26T08:23:34","modified_gmt":"2024-11-26T00:23:34","slug":"java%e6%80%8e%e4%b9%88%e6%8e%a5%e6%94%b6js-%e6%95%b0%e7%bb%84%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37222\/","title":{"rendered":"java\u600e\u4e48\u63a5\u6536js \u6570\u7ec4\u5bf9\u8c61"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u548c java \u4ea4\u4e92\u4e2d\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f20\u9012 javascript \u6570\u7ec4\u5bf9\u8c61\u7ed9 java\uff1a\u4f7f\u7528 json \u5b57\u7b26\u4e32\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a json \u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7 http \u6216 websocket \u53d1\u9001\u5230 java\uff0c\u5e76\u5728 java \u7aef\u4f7f\u7528 gson \u89e3\u6790\u3002\u4f7f\u7528 websocket\uff1a\u5e8f\u5217\u5316\u6570\u7ec4\u5bf9\u8c61\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\uff08\u5982 json\uff09\uff0c\u5e76\u901a\u8fc7 websocket \u8fde\u63a5\u53d1\u9001\u7ed9 java\uff0c\u5e76\u5728 java \u7aef\u53cd\u5e8f\u5217\u5316\u4e3a\u6570\u7ec4\u5bf9\u8c61\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111302451810787.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u63a5\u6536js \u6570\u7ec4\u5bf9\u8c61\u63d2\u56fe\" alt=\"java\u600e\u4e48\u63a5\u6536js \u6570\u7ec4\u5bf9\u8c61\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u63a5\u6536 JavaScript \u6570\u7ec4\u5bf9\u8c61<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 JavaScript \u548c Java \u4ea4\u4e92\u7684\u7f51\u7ad9\u548c\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u9700\u8981\u5728\u4e24\u79cd\u8bed\u8a00\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u5f53\u9700\u8981\u5c06 JavaScript \u4e2d\u7684\u6570\u7ec4\u5bf9\u8c61\u4f20\u9012\u7ed9 Java \u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4f7f\u7528 JSON \u5b57\u7b26\u4e32<\/strong><\/p>\n<ol>\n<li>\u4f7f\u7528 JSON.stringify() \u51fd\u6570\u5c06 JavaScript \u6570\u7ec4\u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u5c06 JSON \u5b57\u7b26\u4e32\u901a\u8fc7 HTTP \u8bf7\u6c42\u6216 WebSockets \u53d1\u9001\u5230 Java \u540e\u7aef\u3002<\/li>\n<\/ol>\n<p><strong>Java \u4ee3\u7801<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import com.google.gson.Gson;\n\n...\n\n\/\/ \u63a5\u6536\u6765\u81ea JavaScript \u7684 JSON \u5b57\u7b26\u4e32\nString jsonString = request.getParameter(\"data\");\n\n\/\/ \u4f7f\u7528 Gson \u5e93\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Java \u6570\u7ec4\u5bf9\u8c61\nGson gson = new Gson();\nint[] array = gson.fromJson(jsonString, int[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 WebSocket<\/strong><\/p>\n<ol>\n<li>\u5728 JavaScript \u4e2d\u4f7f\u7528 WebSocket \u8fde\u63a5\u5230 Java \u540e\u7aef\u3002<\/li>\n<li>\u5e8f\u5217\u5316 JavaScript \u6570\u7ec4\u5bf9\u8c61\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\uff08\u4f8b\u5982 JSON \u6216 Protobuf\uff09\u3002<\/li>\n<li>\u901a\u8fc7 WebSocket \u8fde\u63a5\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u53d1\u9001\u5230 Java\u3002<\/li>\n<\/ol>\n<p><strong>Java \u4ee3\u7801<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.nio.ByteBuffer;\n\n...\n\n\/\/ \u63a5\u6536\u6765\u81ea JavaScript \u7684\u4e8c\u8fdb\u5236\u6570\u636e\nByteBuffer data = webSocket.receive();\n\n\/\/ \u53cd\u5e8f\u5217\u5316\u4e8c\u8fdb\u5236\u6570\u636e\u4e3a Java \u6570\u7ec4\u5bf9\u8c61\n\/\/ \u6839\u636e\u6240\u4f7f\u7528\u7684\u5e8f\u5217\u5316\u683c\u5f0f\u9009\u62e9\u9002\u5f53\u7684\u5e93\u6216\u4ee3\u7801\nint[] array = ...;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u5728\u8de8\u57df\u8bf7\u6c42\u65f6\u8bbe\u7f6e\u9002\u5f53\u7684 CORS \u6807\u5934\u3002<\/li>\n<li>\u9a8c\u8bc1\u63a5\u6536\u7684\u6570\u7ec4\u5bf9\u8c61\u662f\u5426\u6709\u6548\uff0c\u9632\u6b62\u6076\u610f\u6570\u636e\u3002<\/li>\n<li>\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u6c42\u548c\u6027\u80fd\u7684\u4f20\u8f93\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u63a5\u6536js \u6570\u7ec4\u5bf9\u8c61\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u548c java \u4ea4\u4e92\u4e2d\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f20\u9012 javascript \u6570\u7ec4\u5bf9\u8c61\u7ed9 java\uff1a\u4f7f\u7528 json \u5b57\u7b26\u4e32\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a json \u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7 http \u6216 websocket \u53d1\u9001\u5230 java\uff0c\u5e76\u5728 java \u7aef\u4f7f\u7528 gson \u89e3\u6790\u3002\u4f7f\u7528 websocket\uff1a\u5e8f\u5217\u5316\u6570\u7ec4\u5bf9\u8c61\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\uff08\u5982 json\uff09\uff0c\u5e76\u901a\u8fc7 websocket \u8fde\u63a5\u53d1\u9001\u7ed9 java\uff0c\u5e76\u5728 java \u7aef\u53cd\u5e8f\u5217\u5316\u4e3a\u6570\u7ec4\u5bf9\u8c61\u3002 Java \u63a5\u6536 JavaScript \u6570\u7ec4\u5bf9\u8c61 \u5728\u4f7f\u7528 JavaScript \u548c Java \u4ea4\u4e92\u7684\u7f51\u7ad9\u548c\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u9700\u8981\u5728\u4e24\u79cd\u8bed\u8a00\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u5f53\u9700\u8981\u5c06 JavaScript \u4e2d\u7684\u6570\u7ec4\u5bf9\u8c61\u4f20\u9012\u7ed9 Java \u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u4f7f\u7528 JSON \u5b57\u7b26\u4e32 \u4f7f\u7528 JSON.stringify() \u51fd\u6570\u5c06 JavaScript \u6570\u7ec4\u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002 \u5c06 JSON \u5b57\u7b26\u4e32\u901a\u8fc7 HTTP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-37222","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=37222"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37222\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}