{"id":35199,"date":"2024-11-26T11:48:23","date_gmt":"2024-11-26T03:48:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35199\/"},"modified":"2024-11-26T11:48:23","modified_gmt":"2024-11-26T03:48:23","slug":"spring-5-webflux-%e4%b8%ad-dopostandsavelist-%e6%96%b9%e6%b3%95%e8%bf%94%e5%9b%9e-null-%e7%9a%84%e9%97%ae%e9%a2%98%ef%bc%9a%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e5%a4%84%e7%90%86-webclient-%e5%93%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35199\/","title":{"rendered":"Spring 5 WebFlux \u4e2d doPostAndSaveList \u65b9\u6cd5\u8fd4\u56de null \u7684\u95ee\u9898\uff1a\u5982\u4f55\u6b63\u786e\u5904\u7406 WebClient \u54cd\u5e94\u5e76\u8fd4\u56de JSONArray\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173063611893121.jpg\" class=\"aligncenter\" title=\"Spring 5 WebFlux \u4e2d doPostAndSaveList \u65b9\u6cd5\u8fd4\u56de null \u7684\u95ee\u9898\uff1a\u5982\u4f55\u6b63\u786e\u5904\u7406 WebClient \u54cd\u5e94\u5e76\u8fd4\u56de JSONArray\uff1f\u63d2\u56fe\" alt=\"Spring 5 WebFlux \u4e2d doPostAndSaveList \u65b9\u6cd5\u8fd4\u56de null \u7684\u95ee\u9898\uff1a\u5982\u4f55\u6b63\u786e\u5904\u7406 WebClient \u54cd\u5e94\u5e76\u8fd4\u56de JSONArray\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>spring 5 webflux \u4e2d dopostandsavelist \u65b9\u6cd5\u8fd4\u56de\u6570\u636e\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u4ee3\u7801\u4e2d\uff0cdopostandsavelist \u65b9\u6cd5\u88ab\u5b9a\u4e49\u4e3a\u8fd4\u56de mono  \uff0c\u4f46\u5b9e\u9645\u8fd4\u56de\u7684\u5374\u662f null\u3002\u8fd9\u662f\u56e0\u4e3a\u65b9\u6cd5\u4e2d\u5bf9 webclient \u7684\u54cd\u5e94\u8fdb\u884c\u4e86\u8ba2\u9605\uff0c\u800c\u8ba2\u9605\u64cd\u4f5c\u5e76\u4e0d\u4f1a\u8fd4\u56de\u6570\u636e\u3002 <\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u8981\u8ba9 dopostandsavelist \u65b9\u6cd5\u8fd4\u56de\u8bf7\u6c42\u56de\u6765\u7684 flatmap \u4e2d\u7684 jsonarray\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u5c06 string groupsstr \u58f0\u660e\u5757\u79fb\u52a8\u5230 log.info(&#8220;\u8fdb\u5165dopost\u65b9\u6cd5&#8221;); \u8bed\u53e5\u4e0b\u65b9\u3002<\/li>\n<li>\u5c06\u54cd\u5e94\u7684\u5904\u7406\u4ee3\u7801\u653e\u5165 map \u64cd\u4f5c\u7b26\u4e2d\uff0c\u5e76\u7528 return mono.just(jsonobjectjsonarray); \u66ff\u6362 log.info(&#8220;groupsstr:{}&#8221;) \u8bed\u53e5\u3002<\/li>\n<li>\u5c06 if \u5224\u65ad\u5757\u6539\u5199\u4e3a if (jsonobjectjsonarray == null)\uff0c\u5e76\u7528 return mono.empty(); \u66ff\u6362 return null; \u8bed\u53e5\u3002<\/li>\n<li>\u5220\u9664 subscribe(); \u8bed\u53e5\u3002<\/li>\n<\/ol>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>public 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                JSONArray groupsStr = jsonObject.getJSONArray(\"group_chat_list\");\n                log.info(\"groupsStr: {}\", groupsStr);\n                if (groupsStr == null) {\n                    return Mono.empty();\n                } else {\n                    return Mono.just(groupsStr);\n                }\n            });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSpring 5 WebFlux \u4e2d doPostAndSaveList \u65b9\u6cd5\u8fd4\u56de null \u7684\u95ee\u9898\uff1a\u5982\u4f55\u6b63\u786e\u5904\u7406 WebClient \u54cd\u5e94\u5e76\u8fd4\u56de JSONArray\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 dopostandsavelist \u65b9\u6cd5\u8fd4\u56de\u6570\u636e\u7684\u95ee\u9898 \u4ee3\u7801\u4e2d\uff0cdopostandsavelist \u65b9\u6cd5\u88ab\u5b9a\u4e49\u4e3a\u8fd4\u56de mono \uff0c\u4f46\u5b9e\u9645\u8fd4\u56de\u7684\u5374\u662f null\u3002\u8fd9\u662f\u56e0\u4e3a\u65b9\u6cd5\u4e2d\u5bf9 webclient \u7684\u54cd\u5e94\u8fdb\u884c\u4e86\u8ba2\u9605\uff0c\u800c\u8ba2\u9605\u64cd\u4f5c\u5e76\u4e0d\u4f1a\u8fd4\u56de\u6570\u636e\u3002 \u89e3\u51b3\u65b9\u6cd5 \u8981\u8ba9 dopostandsavelist \u65b9\u6cd5\u8fd4\u56de\u8bf7\u6c42\u56de\u6765\u7684 flatmap \u4e2d\u7684 jsonarray\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u5c06 string groupsstr \u58f0\u660e\u5757\u79fb\u52a8\u5230 log.info(&#8220;\u8fdb\u5165dopost\u65b9\u6cd5&#8221;); \u8bed\u53e5\u4e0b\u65b9\u3002 \u5c06\u54cd\u5e94\u7684\u5904\u7406\u4ee3\u7801\u653e\u5165 map \u64cd\u4f5c\u7b26\u4e2d\uff0c\u5e76\u7528 return mono.just(jsonobjectjsonarray); \u66ff\u6362 log.info(&#8220;groupsstr:{}&#8221;) \u8bed\u53e5\u3002 \u5c06 if \u5224\u65ad\u5757\u6539\u5199\u4e3a if (jsonobjectjsonarray == null)\uff0c\u5e76\u7528 return mono.empty(); \u66ff\u6362 return null; \u8bed\u53e5\u3002 \u5220\u9664 subscribe(); \u8bed\u53e5\u3002 \u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a public Mono&lt;JSONArray&gt; doPostAndSaveList(String token, [&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-35199","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35199","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=35199"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35199\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}