{"id":35107,"date":"2024-11-26T13:52:17","date_gmt":"2024-11-26T05:52:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35107\/"},"modified":"2024-11-26T13:52:17","modified_gmt":"2024-11-26T05:52:17","slug":"spring-5-webflux-%e4%b8%ad%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e5%a4%84%e7%90%86-mono-%e8%bf%94%e5%9b%9e%e5%80%bc%e5%b9%b6%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35107\/","title":{"rendered":"Spring 5 WebFlux \u4e2d\u5982\u4f55\u6b63\u786e\u5904\u7406 Mono \u8fd4\u56de\u503c\u5e76\u83b7\u53d6\u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173061397631543.jpg\" class=\"aligncenter\" title=\"Spring 5 WebFlux \u4e2d\u5982\u4f55\u6b63\u786e\u5904\u7406 Mono \u8fd4\u56de\u503c\u5e76\u83b7\u53d6\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"Spring 5 WebFlux \u4e2d\u5982\u4f55\u6b63\u786e\u5904\u7406 Mono \u8fd4\u56de\u503c\u5e76\u83b7\u53d6\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>spring 5 webflux \u4e2d\u8fd4\u56de mono \u7684\u6b63\u786e\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 spring 5 webflux \u4e2d\uff0c\u5904\u7406 http \u8bf7\u6c42\u5e76\u8fd4\u56de mono \u503c\u975e\u5e38\u5e38\u89c1\u3002\u7136\u800c\uff0c\u6709\u65f6\u53ef\u80fd\u9700\u8981\u8fd4\u56de\u8bf7\u6c42\u4ece mono \u6d41\u4e2d\u83b7\u53d6\u7684\u6570\u636e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c06 mono \u6d41\u8f6c\u6362\u6210 flux \u6d41\uff0c\u7136\u540e\u4f7f\u7528 subscribeon() \u65b9\u6cd5\u5c06\u64cd\u4f5c\u63d0\u4ea4\u5230\u53e6\u4e00\u4e2a\u8c03\u5ea6\u5668\u7ebf\u7a0b\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u73b0\u6b64\u64cd\u4f5c\uff1a<\/p>\n<pre>\/**\n * \u6a21\u62df\u670d\u52a1\u7aef\n * @param token\n * @param bodyMap\n * @return\n *\/\npublic Mono&lt;JSONArray&gt; doPostAndSaveList(String token, Map&lt;String, Object&gt; bodyMap) {\n    log.info(\"\u8fdb\u5165doPost\u65b9\u6cd5\");\n    return WebClient.create(baseUrl)\n            .post()\n            .uri(uriBuilder -&gt; uriBuilder.path(uri)\n                    .queryParam(\"access_token\", token)\n                    .build())\n            .bodyValue(bodyMap)\n            .retrieve()\n            .bodyToMono(JSONObject.class)\n            .subscribeOn(Schedulers.elastic())\n            .flatMap(jsonObject -&gt; {\n                String groupsStr = jsonObject.getString(\"group_chat_list\");\n                log.info(\"groupsStr:{}\", groupsStr);\n                List&lt;GroupListResponse&gt; groupListResponses = null;\n                JSONArray jsonObjectJSONArray = jsonObject.getJSONArray(\"group_chat_list\");\n                if (jsonObjectJSONArray != null) {\n                    \/\/ \u6b64\u5904\u53ef\u5bf9 JSONArray \u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\n                    return Mono.just(jsonObjectJSONArray);\n                } else {\n                    return Mono.empty();\n                }\n            });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa webclient \u5e76\u914d\u7f6e\u8bf7\u6c42\u7684 uri \u548c\u53c2\u6570\u3002<\/li>\n<li>\u8bbe\u7f6e\u8bf7\u6c42\u6b63\u6587\u3002<\/li>\n<li>\u8c03\u7528 retrieve() \u65b9\u6cd5\u68c0\u7d22\u54cd\u5e94\u3002<\/li>\n<li>\u5c06\u54cd\u5e94\u8f6c\u6362\u4e3a mono  \uff0c\u6307\u5b9a\u5728 elastic \u8c03\u5ea6\u5668\u7ebf\u7a0b\u4e0a\u6267\u884c\u6b64\u64cd\u4f5c\u3002  <\/li>\n<li>\u5728 flatmap() \u65b9\u6cd5\u4e2d\u63d0\u53d6\u76ee\u6807\u6570\u636e\u5e76\u5c06\u5176\u5305\u88c5\u5230 mono  \u4e2d\u3002  <\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4f7f\u7528 mono.just()\uff0c\u53ef\u4ee5\u5c06 jsonarray \u8f6c\u6362\u6210 mono \u6d41\u3002\u5b8c\u6210\u6b64\u64cd\u4f5c\u540e\uff0c\u65e0\u9700\u518d\u8c03\u7528 subscribe() \u65b9\u6cd5\uff0c\u56e0\u4e3a mono \u6d41\u4e2d\u7684\u6570\u636e\u5c06\u5728\u8fd4\u56de mono \u65f6\u51c6\u5907\u597d\u4f7f\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSpring 5 WebFlux \u4e2d\u5982\u4f55\u6b63\u786e\u5904\u7406 Mono \u8fd4\u56de\u503c\u5e76\u83b7\u53d6\u6570\u636e\uff1f\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>spring 5 webflux \u4e2d\u8fd4\u56de mono \u7684\u6b63\u786e\u65b9\u6cd5 \u5728 spring 5 webflux \u4e2d\uff0c\u5904\u7406 http \u8bf7\u6c42\u5e76\u8fd4\u56de mono \u503c\u975e\u5e38\u5e38\u89c1\u3002\u7136\u800c\uff0c\u6709\u65f6\u53ef\u80fd\u9700\u8981\u8fd4\u56de\u8bf7\u6c42\u4ece mono \u6d41\u4e2d\u83b7\u53d6\u7684\u6570\u636e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c06 mono \u6d41\u8f6c\u6362\u6210 flux \u6d41\uff0c\u7136\u540e\u4f7f\u7528 subscribeon() \u65b9\u6cd5\u5c06\u64cd\u4f5c\u63d0\u4ea4\u5230\u53e6\u4e00\u4e2a\u8c03\u5ea6\u5668\u7ebf\u7a0b\u3002 \u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u73b0\u6b64\u64cd\u4f5c\uff1a \/** * \u6a21\u62df\u670d\u52a1\u7aef * @param token * @param bodyMap * @return *\/ public Mono&lt;JSONArray&gt; doPostAndSaveList(String token, Map&lt;String, Object&gt; bodyMap) { log.info(&#8220;\u8fdb\u5165doPost\u65b9\u6cd5&#8221;); return WebClient.create(baseUrl) .post() .uri(uriBuilder -&gt; uriBuilder.path(uri) .queryParam(&#8220;access_token&#8221;, token) .build()) .bodyValue(bodyMap) [&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-35107","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35107","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=35107"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35107\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}