{"id":3592,"date":"2018-12-19T17:40:11","date_gmt":"2018-12-19T09:40:11","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3592"},"modified":"2018-12-19T17:40:11","modified_gmt":"2018-12-19T09:40:11","slug":"%e8%af%a6%e8%a7%a3-abstracterrorwebexceptionhandler-%e7%b1%bb%e5%9c%a8-webflux-%e5%85%a8%e5%b1%80%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%ad%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3592","title":{"rendered":"\u8be6\u89e3 AbstractErrorWebExceptionHandler \u7c7b\u5728 WebFlux \u5168\u5c40\u5f02\u5e38\u5904\u7406\u4e2d\u7684\u4f7f\u7528"},"content":{"rendered":"<p>\n\t\u770b\u8fc7\u6e90\u7801\u7684\u90fd\u77e5\u9053\uff0c\u6240\u6709\u7684\u6846\u67b6\u57fa\u672c\u4e0a\u90fd\u4f1a\u7ed9\u4f60\u63d0\u4f9b\u591a\u6761\u8def\uff0c\u591a\u4e2a\u5b9e\u73b0\u65b9\u5f0f\u3002WebFlux \u4e5f\u662f\u4e00\u6837\uff0c\u9664\u4e86\u4e0a\u4e00\u7ae0\u4e2d\u7684 DefaultErrorAttributes\uff0c\u5b83\u8fd8\u63d0\u4f9b\u4e86 AbstractErrorWebExceptionHandler\u3002\u672c\u6587\u8bf4\u8bf4 AbstractErrorWebExceptionHandler \u7684\u7528\u6cd5\u3002\n<\/p>\n<p>\n\tAbstractErrorWebExceptionHandler \u770b\u540d\u79f0\u5c31\u77e5\u9053\uff0c\u5b83\u4e5f\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\u3002\u5b83\u63d0\u4f9b\u7684\u529f\u80fd\u66f4\u591a\u4e00\u4e9b\uff0c\u66f4\u5f3a\u5927\u4e00\u4e9b\uff01\n<\/p>\n<p>\n\tAbstractErrorWebExceptionHandler \u4f7f\u7528\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Component\r\n@Order(-2)\r\npublic class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {\r\n    public GlobalErrorWebExceptionHandler(ErrorAttributes errorAttributes,\r\n          ResourceProperties resourceProperties,\r\n          ApplicationContext applicationContext){\r\n        super(errorAttributes,resourceProperties,applicationContext);\r\n    }\r\n    @Override\r\n    protected RouterFunction&lt;ServerResponse&gt; getRoutingFunction(\r\n            ErrorAttributes errorAttributes) {\r\n        return RouterFunctions.route(\r\n                RequestPredicates.all(), this::renderErrorResponse);\r\n    }\r\n    private Mono&lt;ServerResponse&gt; renderErrorResponse(\r\n            ServerRequest request) {\r\n        Map&lt;String, Object&gt; errorPropertiesMap = getErrorAttributes(request, false);\r\n        \/\/ \u8fd9\u91cc\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5904\u7406\u903b\u8f91\r\n        errorPropertiesMap.put(&quot;xttblog&quot;, &quot;www.xttblog.com&quot;);\r\n        return ServerResponse.status(HttpStatus.BAD_REQUEST)\r\n                .contentType(MediaType.APPLICATION_JSON_UTF8)\r\n                .body(BodyInserters.fromObject(errorPropertiesMap));\r\n    }\r\n}<\/pre>\n<p>\n\t\u6ce8\u89e3 @Order(-2) \u8fd9\u4e2a\u6ce8\u89e3\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u6ce8\u89e3\uff0c\u4f60\u81ea\u5b9a\u4e49\u7684\u8fd9\u4e2a\u53ef\u80fd\u4e0d\u4f1a\u751f\u6548\u3002\u56e0\u4e3a WebFlux \u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684\u5b9e\u73b0 DefaultErrorWebExceptionHandler\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u662f @Order(-1)\u3002\u6240\u4ee5\u52a0\u4e0a @Order(-2) \u662f\u8ba9\u6211\u4eec\u81ea\u5df1\u7684\u5b9e\u73b0\u62e5\u6709\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7\u3002\n<\/p>\n<p>\n\terrorAttributes \u5bf9\u8c61\u5c06\u662f\u6211\u4eec\u5728\u7f51\u7edc\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u7684\u6784\u9020\u51fd\u6570\u4f20\u9012\u4e00\u4e2a\u7684\u7cbe\u786e\u526f\u672c\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5e94\u8be5\u662f\u6211\u4eec\u81ea\u5b9a\u4e49\u7684 Error Attributes \u7c7b\u3002\u7136\u540e\uff0c\u6211\u4eec\u6e05\u695a\u5730\u8868\u660e\u6211\u4eec\u60f3\u8981\u5c06\u6240\u6709\u9519\u8bef\u5904\u7406\u8bf7\u6c42\u8def\u7531\u5230 renderErrorResponse() \u65b9\u6cd5\u3002\u6700\u540e\uff0c\u6211\u4eec\u83b7\u53d6\u9519\u8bef\u5c5e\u6027\u5e76\u5c06\u5b83\u4eec\u63d2\u5165\u670d\u52a1\u5668\u54cd\u5e94\u4e3b\u4f53\u4e2d\u3002\n<\/p>\n<p>\n\t\u7136\u540e\uff0c\u751f\u6210\u4e00\u4e2a JSON \u54cd\u5e94\uff0c\u5176\u4e2d\u5305\u542b\u9519\u8bef\uff0cHTTP \u72b6\u6001\u548c\u8ba1\u7b97\u673a\u5ba2\u6237\u7aef\u5f02\u5e38\u6d88\u606f\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u5bf9\u4e8e\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u5b83\u6709\u4e00\u4e2a whitelabel \u9519\u8bef\u5904\u7406\u7a0b\u5e8f\uff0c\u5b83\u4ee5 HTML \u683c\u5f0f\u5448\u73b0\u76f8\u540c\u7684\u6570\u636e\u3002\u5176\u4e2d\u7684\u54cd\u5e94\u4fe1\u606f\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u8fdb\u884c\u5b9a\u5236\u3002\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/www.baeldung.com\/spring-webflux-errors\" name=\"Handling Errors in Spring WebFlux\" target=\"_blank\" title=\"\">Handling Errors in Spring WebFlux<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3592\">\u8be6\u89e3 AbstractErrorWebExceptionHandler \u7c7b\u5728 WebFlux \u5168\u5c40\u5f02\u5e38\u5904\u7406\u4e2d\u7684\u4f7f\u7528<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u770b\u8fc7\u6e90\u7801\u7684\u90fd\u77e5\u9053\uff0c\u6240\u6709\u7684\u6846\u67b6\u57fa\u672c\u4e0a\u90fd\u4f1a\u7ed9\u4f60\u63d0\u4f9b\u591a\u6761\u8def\uff0c\u591a\u4e2a\u5b9e\u73b0\u65b9\u5f0f\u3002WebFlux \u4e5f\u662f\u4e00\u6837\uff0c\u9664\u4e86\u4e0a\u4e00\u7ae0\u4e2d\u7684  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1841,1701,946,785,906],"class_list":["post-3592","post","type-post","status-publish","format-standard","hentry","category-java","tag-abstracterrorwebexceptionhandler","tag-webflux","tag-946","tag-785","tag-906"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3592"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3592\/revisions"}],"predecessor-version":[{"id":3593,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3592\/revisions\/3593"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}