{"id":7903,"date":"2023-11-28T20:06:13","date_gmt":"2023-11-28T12:06:13","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7903"},"modified":"2023-12-05T15:57:18","modified_gmt":"2023-12-05T07:57:18","slug":"springboot-yi-chang-chu-li-de-fang-shi-ni-zhi-dao","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-yi-chang-chu-li-de-fang-shi-ni-zhi-dao\/","title":{"rendered":"SpringBoot\u5f02\u5e38\u5904\u7406\u7684\u65b9\u5f0f\u4f60\u77e5\u9053\u51e0\u79cd\uff1f"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u65b9\u9762\uff0c\u800c\u4e14\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5904\u7406\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 Spring Boot \u5f02\u5e38\u5904\u7406\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<strong>\u4f7f\u7528 <code>@ControllerAdvice<\/code> \u6ce8\u89e3\uff1a<\/strong><\/p>\n<ul>\n<li><code>@ControllerAdvice<\/code> \u6ce8\u89e3\u7528\u4e8e\u5b9a\u4e49\u5168\u5c40\u5f02\u5e38\u5904\u7406\u5668\u3002\u901a\u8fc7\u5728\u4e00\u4e2a\u7c7b\u4e0a\u6807\u8bb0 <code>@ControllerAdvice<\/code> \u6ce8\u89e3\uff0c\u53ef\u4ee5\u96c6\u4e2d\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.http.HttpStatus;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.web.bind.annotation.ControllerAdvice;\nimport org.springframework.web.bind.annotation.ExceptionHandler;\n\n@ControllerAdvice\npublic class GlobalExceptionHandler {\n\n    @ExceptionHandler(Exception.class)\n    public ResponseEntity&lt;String&gt; handleException(Exception e) {\n        return new ResponseEntity&lt;&gt;(&quot;An error occurred: &quot; + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528 <code>@ExceptionHandler<\/code> \u6ce8\u89e3\uff1a<\/strong><\/p>\n<ul>\n<li><code>@ExceptionHandler<\/code> \u6ce8\u89e3\u7528\u4e8e\u5728\u7279\u5b9a\u7684 Controller \u7c7b\u4e2d\u5904\u7406\u5f02\u5e38\u3002\u901a\u8fc7\u5728 Controller \u7c7b\u7684\u65b9\u6cd5\u4e0a\u6807\u8bb0\u8be5\u6ce8\u89e3\uff0c\u53ef\u4ee5\u5904\u7406\u7279\u5b9a\u5f02\u5e38\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.http.HttpStatus;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.web.bind.annotation.ExceptionHandler;\nimport org.springframework.web.bind.annotation.ResponseStatus;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\npublic class MyController {\n\n    @ExceptionHandler(RuntimeException.class)\n    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)\n    public ResponseEntity&lt;String&gt; handleRuntimeException(RuntimeException e) {\n        return new ResponseEntity&lt;&gt;(&quot;An error occurred: &quot; + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528 <code>ResponseEntityExceptionHandler<\/code>\uff1a<\/strong><\/p>\n<ul>\n<li>\u7ee7\u627f <code>ResponseEntityExceptionHandler<\/code> \u7c7b\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5904\u7406\u5f02\u5e38\u7684\u65b9\u6cd5\uff0c\u5e76\u4f7f\u7528 <code>@ExceptionHandler<\/code> \u6ce8\u89e3\u6807\u8bb0\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.http.ResponseEntity;\nimport org.springframework.web.bind.annotation.ControllerAdvice;\nimport org.springframework.web.bind.annotation.ExceptionHandler;\nimport org.springframework.web.bind.annotation.ResponseStatus;\nimport org.springframework.web.bind.annotation.RestController;\nimport org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;\n\n@ControllerAdvice\n@RestController\npublic class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {\n\n    @ExceptionHandler(Exception.class)\n    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)\n    public ResponseEntity&lt;String&gt; handleException(Exception e) {\n        return new ResponseEntity&lt;&gt;(&quot;An error occurred: &quot; + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528 <code>@RestControllerAdvice<\/code>\uff1a<\/strong><\/p>\n<ul>\n<li><code>@RestControllerAdvice<\/code> \u662f <code>@ControllerAdvice<\/code> \u548c <code>@ResponseBody<\/code> \u7684\u7ec4\u5408\uff0c\u7528\u4e8e\u5b9a\u4e49\u5168\u5c40\u5f02\u5e38\u5904\u7406\u5668\uff0c\u5e76\u76f4\u63a5\u8fd4\u56de JSON \u683c\u5f0f\u7684\u54cd\u5e94\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.http.HttpStatus;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.web.bind.annotation.ExceptionHandler;\nimport org.springframework.web.bind.annotation.ResponseStatus;\nimport org.springframework.web.bind.annotation.RestControllerAdvice;\n\n@RestControllerAdvice\npublic class GlobalRestControllerAdvice {\n\n    @ExceptionHandler(Exception.class)\n    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)\n    public ResponseEntity&lt;String&gt; handleException(Exception e) {\n        return new ResponseEntity&lt;&gt;(&quot;An error occurred: &quot; + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u65b9\u5f0f\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\uff0c\u901a\u5e38\u5168\u5c40\u5f02\u5e38\u5904\u7406\u5668\u9002\u7528\u4e8e\u5904\u7406\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\uff0c\u800c\u5c40\u90e8\u7684 <code>@ExceptionHandler<\/code> \u5219\u9002\u7528\u4e8e\u5904\u7406\u7279\u5b9a Controller \u6216\u65b9\u6cd5\u4e2d\u7684\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u65b9\u9762\uff0c\u800c\u4e14\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5904\u7406\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 Spr [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-7903","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=7903"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7903\/revisions"}],"predecessor-version":[{"id":16552,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7903\/revisions\/16552"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7903"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}