{"id":7587,"date":"2023-11-28T19:40:54","date_gmt":"2023-11-28T11:40:54","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7587"},"modified":"2023-12-05T15:37:31","modified_gmt":"2023-12-05T07:37:31","slug":"springboot-zhong-ru-he-shi-xian-quan-ju-yi-chang-c","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-ru-he-shi-xian-quan-ju-yi-chang-c\/","title":{"rendered":"SpringBoot\u4e2d\u5982\u4f55\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\uff1f"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\u901a\u5e38\u6d89\u53ca\u5230\u4f7f\u7528 <code>@ControllerAdvice<\/code> \u6ce8\u89e3\u7684\u5f02\u5e38\u5904\u7406\u7c7b\u3002<code>@ControllerAdvice<\/code> \u6ce8\u89e3\u7528\u4e8e\u5b9a\u4e49\u5168\u5c40\u6027\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u96c6\u4e2d\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<strong>\u521b\u5efa\u5f02\u5e38\u5904\u7406\u7c7b\uff1a<\/strong> \u521b\u5efa\u4e00\u4e2a\u7c7b\u5e76\u4f7f\u7528 <code>@ControllerAdvice<\/code> \u6ce8\u89e3\u6807\u6ce8\uff0c\u8be5\u7c7b\u4e2d\u7684\u65b9\u6cd5\u5c06\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.http.HttpStatus;\nimport org.springframework.ui.Model;\nimport org.springframework.web.bind.annotation.ControllerAdvice;\nimport org.springframework.web.bind.annotation.ExceptionHandler;\nimport org.springframework.web.bind.annotation.ResponseStatus;\n\n@ControllerAdvice\npublic class GlobalExceptionHandler {\n\n    @ExceptionHandler(Exception.class)\n    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)\n    public String handleException(Exception e, Model model) {\n        model.addAttribute(&quot;error&quot;, e.getMessage());\n        return &quot;error&quot;;\n    }\n\n    \/\/ Add more @ExceptionHandler methods for specific exceptions if needed\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>handleException<\/code> \u65b9\u6cd5\u5904\u7406\u6240\u6709\u7c7b\u578b\u7684 <code>Exception<\/code>\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e26\u6709\u9519\u8bef\u4fe1\u606f\u7684\u89c6\u56fe\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684 <code>@ExceptionHandler<\/code> \u65b9\u6cd5\u6765\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002\n<\/li>\n<li>\n<strong>\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\uff1a<\/strong> \u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u663e\u793a\u5f02\u5e38\u4fe1\u606f\u7684\u9519\u8bef\u9875\u9762\uff0c\u6bd4\u5982 <code>error.html<\/code>\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>handleException<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u662f &quot;error&quot; \u5b57\u7b26\u4e32\uff0cSpring Boot \u5c06\u5c1d\u8bd5\u5bfb\u627e\u5bf9\u5e94\u7684\u89c6\u56fe\u6a21\u677f\u3002<\/p>\n<pre><code class=\"language-html\">&lt;!-- src\/main\/resources\/templates\/error.html --&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;Error&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Error&lt;\/h1&gt;\n    &lt;p th:text=&quot;${error}&quot;&gt;&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u9875\u9762\u5c06\u663e\u793a\u5f02\u5e38\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\n<\/li>\n<li>\n<strong>\u914d\u7f6e\u5168\u5c40\u5f02\u5e38\u5904\u7406\uff1a<\/strong> \u5728 Spring Boot \u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u6dfb\u52a0 <code>@EnableWebMvc<\/code> \u6ce8\u89e3\u4ee5\u542f\u7528\u5168\u5c40\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.context.annotation.EnableAspectJAutoProxy;\nimport org.springframework.web.servlet.config.annotation.EnableWebMvc;\n\n@SpringBootApplication\n@EnableWebMvc\n@EnableAspectJAutoProxy\npublic class MyApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(MyApplication.class, args);\n    }\n}\n<\/code><\/pre>\n<p><code>@EnableWebMvc<\/code> \u6ce8\u89e3\u542f\u7528\u4e86 Spring Boot \u7684 MVC \u914d\u7f6e\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\u7528 <code>@ControllerAdvice<\/code> \u6765\u5b9a\u4e49\u5168\u5c40\u5f02\u5e38\u5904\u7406\u3002\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\u3002\u5f53\u5f02\u5e38\u53d1\u751f\u65f6\uff0cSpring Boot \u4f1a\u8c03\u7528\u76f8\u5e94\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\uff0c\u5e76\u5728\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u9875\u9762\u4e2d\u663e\u793a\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\u901a\u5e38\u6d89\u53ca\u5230\u4f7f\u7528 @ControllerAdvice \u6ce8\u89e3\u7684\u5f02\u5e38 [&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-7587","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7587","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=7587"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7587\/revisions"}],"predecessor-version":[{"id":16525,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7587\/revisions\/16525"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7587"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}