{"id":1157,"date":"2020-09-01T17:30:59","date_gmt":"2020-09-01T09:30:59","guid":{"rendered":"http:\/\/diuut.com\/?p=1157"},"modified":"2020-09-01T17:31:02","modified_gmt":"2020-09-01T09:31:02","slug":"%e5%9c%a8webflux%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e5%bc%82%e5%b8%b8%e5%93%8d%e5%ba%94","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=1157","title":{"rendered":"\u5728WebFlux\u4e2d\u81ea\u5b9a\u4e49\u5f02\u5e38\u54cd\u5e94"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42\uff1a\u51fa\u73b0\u610f\u5916\u62a5\u9519\u54cd\u5e94\u5185\u5bb9\u53ef\u8bfb\u6027\u5dee\uff0c\u4e0d\u5229\u4e8e\u6392\u67e5\u4e0e\u5b9a\u4f4d\uff0c\u9700\u8981\u81ea\u5b9a\u4e49\u9519\u8bef\u54cd\u5e94\u3002<\/p><\/blockquote>\n\n\n\n<p>\u5728web\u4e2d\u53ef\u4ee5\u4f7f\u7528@ControllerAdvice\u5373\u53ef\u7f16\u5199\u7edf\u4e00\u5f02\u5e38\u54cd\u5e94\uff0c\u5728webFlux\u4e0b\u5219\u662f\u53e6\u4e00\u79cd\u7f16\u5199\u65b9\u5f0f\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nComponent\n@Order(-2) \/\/\u8fd9\u91cc\u5c06\u5168\u5c40\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u7684\u987a\u5e8f\u8bbe\u7f6e\u4e3a-2\u3002\u8fd9\u662f\u4e3a\u4e86\u8ba9\u5b83\u6bd4 @Order(-1) \u6ce8\u518c\u7684 DefaultErrorWebExceptionHandler \u5904\u7406\u7a0b\u5e8f\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7\u3002\n@Slf4j\npublic class ExceptionHandler implements ErrorWebExceptionHandler {\n    @SneakyThrows\n    @Override\n    public Mono&lt;Void&gt; handle(ServerWebExchange serverWebExchange, Throwable throwable) {\n        ServerHttpResponse response = serverWebExchange.getResponse();\n        response.setStatusCode(HttpStatus.OK);\n        Dson dson = new Dson();\n        String message = throwable.getMessage();\n        dson.put(&quot;\u9519\u8bef\u4fe1\u606f:&quot;, message);\n        String methodValue = serverWebExchange.getRequest().getMethodValue();\n        dson.put(&quot;\u8bf7\u6c42\u65b9\u5f0f:&quot;, methodValue);\n        return serverWebExchange.getRequest().getBody()\n                .collectList()\n                .flatMap(list -&gt; {\n                    log.info(&quot;list:{}&quot;, list);\n                    StringBuffer str = new StringBuffer();\n                    for (DataBuffer buf : list) {\n                        byte&#91;] bytes = new byte&#91;buf.readableByteCount()];\n                        buf.read(bytes);\n                        str.append(new String(bytes));\n                    }\n                    dson.put(&quot;\u6d88\u606f\u4f53:&quot;, str.toString());\n                    response.getHeaders().setContentType(MediaType.APPLICATION_JSON);\n                    DataBuffer buff = response.bufferFactory().wrap(dson._Value().toString().getBytes());\/\/\u975e\u6c60\u5316\u5185\u5b58\n                    return response.writeWith(Mono.just(buff));\n                });\n\n    }\n}\n<\/pre><\/div>\n\n\n<h4> \u6548\u679c\u6f14\u793a <\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u8bf7\u6c42\u65b9\u5f0f\uff1apost  \n\u8bf7\u6c42\u5730\u5740\uff1alocalhost:11000\/error\n\u8bf7\u6c42\u4f53:\n{\n\t&quot;\u8d26\u53f7&quot;:&quot;152xxxxxxx&quot;,\n\t&quot;token&quot;:&quot;356434371b1a7a984d37a1f4f3054c8b&quot;,\n}\n\n<\/pre><\/div>\n\n\n<p>\u8bf7\u6c42\u65b9\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    @RequestMapping(&quot;\/error&quot;)\n    public Object getError(String Str) {\n        return 1\/0;\n    }\n<\/pre><\/div>\n\n\n<p>\u539f\u751f\u9519\u8bef\u54cd\u5e94\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n{\n    &quot;timestamp&quot;: 1598952492292,\n    &quot;path&quot;: &quot;\/error&quot;,\n    &quot;status&quot;: 500,\n    &quot;error&quot;: &quot;Internal Server Error&quot;,\n    &quot;message&quot;: &quot;\/ by zero&quot;,\n    &quot;requestId&quot;: &quot;af25e175-1&quot;\n}\n<\/pre><\/div>\n\n\n<p>\u81ea\u5b9a\u4e49\u9519\u8bef\u54cd\u5e94\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n{\n    &quot;\u9519\u8bef\u4fe1\u606f:&quot;: &quot;\/ by zero&quot;,\n    &quot;\u8bf7\u6c42\u65b9\u5f0f:&quot;: &quot;POST&quot;,\n    &quot;\u6d88\u606f\u4f53:&quot;: {\n        &quot;\u8d26\u53f7&quot;: &quot;152xxxxxxxx&quot;,\n        &quot;token&quot;: &quot;356434371b1a7a984d37a1f4f3054c8b&quot;\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a\u51fa\u73b0\u610f\u5916\u62a5\u9519\u54cd\u5e94\u5185\u5bb9\u53ef\u8bfb\u6027\u5dee\uff0c\u4e0d\u5229\u4e8e\u6392\u67e5\u4e0e\u5b9a\u4f4d\uff0c\u9700\u8981\u81ea\u5b9a\u4e49\u9519\u8bef\u54cd\u5e94\u3002 \u5728web\u4e2d\u53ef\u4ee5\u4f7f\u7528@Control<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=1157\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">\u5728WebFlux\u4e2d\u81ea\u5b9a\u4e49\u5f02\u5e38\u54cd\u5e94<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":1147,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1157"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1157"}],"version-history":[{"count":2,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1157\/revisions"}],"predecessor-version":[{"id":1159,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1157\/revisions\/1159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/1147"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}