{"id":3586,"date":"2018-12-19T11:49:07","date_gmt":"2018-12-19T03:49:07","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3586"},"modified":"2018-12-19T11:49:07","modified_gmt":"2018-12-19T03:49:07","slug":"%e4%b8%80%e7%bd%91%e6%89%93%e5%b0%bd-exceptionhandler%e3%80%81handlerexceptionresolver%e3%80%81controlleradvice-%e4%b8%89%e5%85%84%e5%bc%9f%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3586","title":{"rendered":"\u4e00\u7f51\u6253\u5c3d @ExceptionHandler\u3001HandlerExceptionResolver\u3001@controlleradvice \u4e09\u5144\u5f1f\uff01"},"content":{"rendered":"<p>\n\t\u628a @ExceptionHandler\u3001HandlerExceptionResolver\u3001@controlleradvice \u4e09\u5144\u5f1f\u653e\u5728\u4e00\u8d77\u6765\u5199\u66f4\u6709\u6bd4\u8f83\u6027\u3002\u8fd9\u4e09\u4e2a\u4e1c\u897f\u90fd\u662f\u7528\u6765\u5904\u7406\u5f02\u5e38\u7684\uff0c\u4f46\u662f\u5b83\u4eec\u4f7f\u7528\u7684\u573a\u666f\u90fd\u4e0d\u4e00\u6837\u3002\u770b\u672c\u6587\u7ed9\u4f60\u8be6\u7ec6\u7684\u8bb2\u89e3\uff0c\u518d\u4e5f\u4e0d\u6015\u9762\u8bd5\u88ab\u95ee\u5230\u4e86\uff01\n<\/p>\n<p>\n\t\u8fd9\u4e09\u4e2a\u6ce8\u89e3\u90fd\u662f\u6765\u81ea\u4e8e SpringMVC \u7684\uff0c\u90fd\u80fd\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002\n<\/p>\n<p>\n\tJava \u7a0b\u5e8f\u5458\u90fd\u975e\u5e38\u7684\u75db\u6068\u5f02\u5e38\uff0c\u5f88\u591a\u4eba\u8ba8\u538c Java \u5c31\u662f\u56e0\u4e3a\u5b83\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002\u5230\u5904\u7684 try-catch-finally\uff0c\u518d\u4e0d\u662f\u5c31\u662f\u5230\u5904\u629b\u51fa\u5f02\u5e38\u3002\n<\/p>\n<p>\n\t\u6240\u4ee5 Spring \u6df1\u77e5 Java \u7684\u75bc\u70b9\uff0c\u63a8\u51fa\u4e86\uff1a@ExceptionHandler\u3001HandlerExceptionResolver\u3001@controlleradvice \u6765\u65b9\u4fbf\u6211\u4eec\u5904\u7406\u4e00\u4e9b\u5f02\u5e38\uff01\n<\/p>\n<h2>\n\t@ExceptionHandler \u6ce8\u89e3<br \/>\n<\/h2>\n<p>\n\t\u7528\u4e8e\u5c40\u90e8\u65b9\u6cd5\u6355\u83b7\uff0c\u4e0e\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u5904\u4e8e\u540c\u4e00\u4e2a Controller \u7c7b\u3002\u6e90\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Target({ElementType.METHOD})\r\n@Retention(RetentionPolicy.RUNTIME)\r\n@Documented\r\npublic @interface ExceptionHandler {\r\n    Class&lt;? extends Throwable&gt;[] value() default {};\r\n}<\/pre>\n<p>\n\t\u4ece\u6e90\u7801\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c@ExceptionHandler \u6ce8\u89e3\u53ea\u80fd\u4f5c\u7528\u4e3a\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e0a\uff0c\u5e76\u4e14\u5728\u8fd0\u884c\u65f6\u6709\u6548\uff0cvalue() \u53ef\u4ee5\u6307\u5b9a\u5f02\u5e38\u7c7b\u3002\u7531\u8be5\u6ce8\u89e3\u6ce8\u91ca\u7684\u65b9\u6cd5\u53ef\u4ee5\u5177\u6709\u7075\u6d3b\u7684\u8f93\u5165\u53c2\u6570\u3002\n<\/p>\n<p>\n\t\u5f02\u5e38\u53c2\u6570\u53ef\u4ee5\u5305\u62ec\u4e00\u822c\u7684\u5f02\u5e38\u6216\u7279\u5b9a\u7684\u5f02\u5e38\uff08\u5373\u81ea\u5b9a\u4e49\u5f02\u5e38\uff09\uff0c\u5982\u679c\u6ce8\u89e3\u6ca1\u6709\u6307\u5b9a\u5f02\u5e38\u7c7b\uff0c\u4f1a\u9ed8\u8ba4\u8fdb\u884c\u6620\u5c04\u3002\n<\/p>\n<p>\n\t\u7528\u6cd5\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Controller\r\npublic class XttblogController {\r\n    @ExceptionHandler({NullPointerException.class})\r\n    public String exception(NullPointerException e) {\r\n        System.out.println(e.getMessage());\r\n        e.printStackTrace();\r\n        return &quot;null pointer exception&quot;;\r\n    }\r\n    @RequestMapping(&quot;test&quot;)\r\n    public void test() {\r\n        throw new NullPointerException(&quot;\u51fa\u9519\u4e86\uff01&quot;);\r\n    }\r\n}<\/pre>\n<p>\n\t\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u53ea\u4f1a\u6355\u83b7 XttblogController \u7c7b\u4e2d\u7684 NullPointerException \u5f02\u5e38\u3002\n<\/p>\n<h2>\n\tHandlerExceptionResolver \u63a5\u53e3<br \/>\n<\/h2>\n<p>\n\tHandlerExceptionResolver \u662f Spring \u63d0\u4f9b\u7684\u4e00\u4e2a\u63a5\u53e3\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5168\u5c40\u5f02\u5e38\uff01\n<\/p>\n<pre class=\"brush:java;\">\r\n@Component\r\npublic class GlobalExceptionResolver implements HandlerExceptionResolver{\r\n&nbsp; &nbsp; private ObjectMapper objectMapper;\r\n&nbsp; &nbsp; public CustomMvcExceptionHandler() {\r\n&nbsp; &nbsp; &nbsp; &nbsp; objectMapper = new ObjectMapper();\r\n&nbsp; &nbsp; }\r\n&nbsp; &nbsp; @Override\r\n&nbsp; &nbsp; public ModelAndView resolveException(HttpServletRequest request,&nbsp;\r\n&nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse response,Object o, Exception ex) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; response.setStatus(200);\r\n&nbsp; &nbsp; &nbsp; &nbsp; response.setContentType(MediaType.APPLICATION_JSON_VALUE);\r\n&nbsp; &nbsp; &nbsp; &nbsp; response.setCharacterEncoding(&quot;UTF-8&quot;);\r\n&nbsp; &nbsp; &nbsp; &nbsp; response.setHeader(&quot;Cache-Control&quot;, &quot;no-cache, must-revalidate&quot;);\r\n&nbsp; &nbsp; &nbsp; &nbsp; Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();\r\n&nbsp; &nbsp; &nbsp; &nbsp; if (ex instanceof NullPointerException) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(&quot;code&quot;, ResponseCode.NP_EXCEPTION);\r\n&nbsp; &nbsp; &nbsp; &nbsp; } else if (ex instanceof IndexOutOfBoundsException) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(&quot;code&quot;, ResponseCode.INDEX_OUT_OF_BOUNDS_EXCEPTION);\r\n&nbsp; &nbsp; &nbsp; &nbsp; } else {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(&quot;code&quot;, ResponseCode.CATCH_EXCEPTION);\r\n&nbsp; &nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; &nbsp; &nbsp; try {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(&quot;data&quot;, ex.getMessage());\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.getWriter().write(objectMapper.writeValueAsString(map));\r\n&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();\r\n&nbsp; &nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; &nbsp; &nbsp; return new ModelAndView();\r\n&nbsp; &nbsp; }\r\n}<\/pre>\n<p>\n\t\u5728 Spring \u6e90\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\u5b83\u4f1a\u83b7\u53d6\u4e00\u4e2a\u5b9e\u73b0\u4e86 HandlerExceptionResolver \u63a5\u53e3\u7684\u5217\u8868 List&lt;HandlerExceptionResolver&gt; resolvers; \u5982\u679c\u8fd9\u4e2a\u5217\u8868\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5faa\u73af\u5904\u7406\u5176\u4e2d\u7684\u5f02\u5e38\u3002\n<\/p>\n<p>\n\tHandlerExceptionResolve \u867d\u7136\u80fd\u591f\u5904\u7406\u5168\u5c40\u5f02\u5e38\uff0c\u4f46\u662f Spring \u5b98\u65b9\u4e0d\u63a8\u8350\u4f7f\u7528\u5b83\u3002\n<\/p>\n<h2>\n\t@controlleradvice \u6ce8\u89e3<br \/>\n<\/h2>\n<p>\n\t\u53e6\u5916\u4e00\u4e2a\u80fd\u591f\u5904\u7406\u5168\u5c40\u5f02\u5e38\u7684\u5c31\u662f @controlleradvice \u6ce8\u89e3\u4e86\u3002\n<\/p>\n<p>\n\t@controlleradvice \u6ce8\u89e3\u6839\u636e\u5b83\u7684\u6e90\u7801\uff0c\u6211\u4eec\u77e5\u9053\u5b83\u4e5f\u53ea\u80fd\u4f5c\u7528\u5728\u7c7b\u4e0a\uff0c\u5e76\u4e14\u4f5c\u7528\u4e8e\u8fd0\u884c\u65f6\u3002\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n@ControllerAdvice\r\npublic class ExceptionController {\r\n    @ExceptionHandler(RuntimeException.class)\r\n    public ModelAndView handlerRuntimeException(RuntimeException ex) {\r\n        if (ex instanceof MaxUploadSizeExceededException) {\r\n            return new ModelAndView(&quot;error&quot;).addObject(&quot;msg&quot;, &quot;\u6587\u4ef6\u592a\u5927\uff01&quot;);\r\n        }\r\n        return new ModelAndView(&quot;error&quot;).addObject(&quot;msg&quot;, &quot;\u672a\u77e5\u9519\u8bef\uff1a&quot; + ex);\r\n    }\r\n    @ExceptionHandler(Exception.class)\r\n    public ModelAndView handlerMaxUploadSizeExceededException(Exception ex) {\r\n        if (ex != null) {\r\n            return new ModelAndView(&quot;error&quot;).addObject(&quot;msg&quot;, ex);\r\n        }\r\n        return new ModelAndView(&quot;error&quot;).addObject(&quot;msg&quot;, &quot;\u672a\u77e5\u9519\u8bef\uff1a&quot; + ex);\r\n    }\r\n}<\/pre>\n<p>\n\t\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c@ControllerAdvice \u4e00\u822c\u662f\u548c @ExceptionHandler \u7ec4\u5408\u5728\u4e00\u8d77\u4f7f\u7528\u7684\u3002\u5b98\u65b9\u4e5f\u63a8\u8350\u7528\u8fd9\u79cd\u65b9\u5f0f\u5904\u7406\u7edf\u4e00\u5168\u5c40\u5f02\u5e38\u3002<\/p>\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=3586\">\u4e00\u7f51\u6253\u5c3d @ExceptionHandler\u3001HandlerExceptionResolver\u3001@controlleradvice \u4e09\u5144\u5f1f\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u628a @ExceptionHandler\u3001HandlerExceptionResolver\u3001@controlle [&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":[1839,1837,1838,551,414],"class_list":["post-3586","post","type-post","status-publish","format-standard","hentry","category-java","tag-controlleradvice","tag-exceptionhandler","tag-handlerexceptionresolver","tag-spring"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3586","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=3586"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3586\/revisions"}],"predecessor-version":[{"id":3587,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3586\/revisions\/3587"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}