{"id":9758,"date":"2023-11-28T21:10:17","date_gmt":"2023-11-28T13:10:17","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9758"},"modified":"2023-12-06T17:37:13","modified_gmt":"2023-12-06T09:37:13","slug":"springcloud-zhong-ru-he-shi-xian-quan-ju-yi-chang","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springcloud-zhong-ru-he-shi-xian-quan-ju-yi-chang\/","title":{"rendered":"SpringCloud\u4e2d\u5982\u4f55\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\uff1f"},"content":{"rendered":"<p>\u5728 Spring Cloud \u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\u901a\u5e38\u662f\u901a\u8fc7\u4f7f\u7528 Spring Boot \u63d0\u4f9b\u7684 <code>@ControllerAdvice<\/code> \u548c <code>@ExceptionHandler<\/code> \u6ce8\u89e3\u6765\u5b8c\u6210\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<strong>\u521b\u5efa\u5168\u5c40\u5f02\u5e38\u5904\u7406\u7c7b\uff1a<\/strong><br \/>\n\u521b\u5efa\u4e00\u4e2a\u7c7b\u5e76\u4f7f\u7528 <code>@ControllerAdvice<\/code> \u6ce8\u89e3\u6807\u6ce8\uff0c\u540c\u65f6\u5b9a\u4e49 <code>@ExceptionHandler<\/code> \u65b9\u6cd5\u6765\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u8fd9\u4e2a\u7c7b\u5c06\u6355\u83b7\u6240\u6709\u88ab <code>@Controller<\/code> \u6807\u6ce8\u7684\u7c7b\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\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        \/\/ \u5904\u7406\u5f02\u5e38\u903b\u8f91\uff0c\u53ef\u4ee5\u8bb0\u5f55\u65e5\u5fd7\u7b49\n        return new ResponseEntity&lt;&gt;(&quot;An error occurred: &quot; + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n\n    \/\/ \u53ef\u4ee5\u5b9a\u4e49\u5176\u4ed6 @ExceptionHandler \u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u914d\u7f6e\u5f02\u5e38\u8fd4\u56de\u683c\u5f0f\uff08\u53ef\u9009\uff09\uff1a<\/strong><br \/>\n\u53ef\u4ee5\u914d\u7f6e\u5168\u5c40\u5f02\u5e38\u5904\u7406\u7c7b\u8fd4\u56de\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\uff0c\u8fd4\u56de JSON \u683c\u5f0f\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;\n\n@Configuration\npublic class WebConfig {\n\n    @Bean\n    public MappingJackson2HttpMessageConverter customJsonMessageConverter() {\n        return new CustomJsonMessageConverter();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5b9a\u4e49\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a<\/strong><br \/>\n\u5728\u5e94\u7528\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u7ee7\u627f\u81ea <code>RuntimeException<\/code> \u6216\u5176\u5b50\u7c7b\uff0c\u5e76\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-java\">public class CustomException extends RuntimeException {\n\n    public CustomException(String message) {\n        super(message);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5728\u4e1a\u52a1\u4ee3\u7801\u4e2d\u629b\u51fa\u5f02\u5e38\uff1a<\/strong><br \/>\n\u5728\u4e1a\u52a1\u4ee3\u7801\u4e2d\uff0c\u5f53\u53d1\u751f\u9700\u8981\u5904\u7406\u7684\u5f02\u5e38\u60c5\u51b5\u65f6\uff0c\u629b\u51fa\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\n@RequestMapping(&quot;\/example&quot;)\npublic class ExampleController {\n\n    @Autowired\n    private ExampleService exampleService;\n\n    @GetMapping(&quot;\/trigger-error&quot;)\n    public ResponseEntity&lt;String&gt; triggerError() {\n        \/\/ \u4e1a\u52a1\u903b\u8f91\u4e2d\u53d1\u751f\u5f02\u5e38\n        exampleService.performAction();\n        return ResponseEntity.ok(&quot;Success&quot;);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u5f53\u4e1a\u52a1\u4ee3\u7801\u4e2d\u629b\u51fa\u5f02\u5e38\u65f6\uff0c\u5168\u5c40\u5f02\u5e38\u5904\u7406\u7c7b\u5c06\u6355\u83b7\u8be5\u5f02\u5e38\uff0c\u5e76\u6839\u636e <code>@ExceptionHandler<\/code> \u65b9\u6cd5\u7684\u903b\u8f91\u8fd4\u56de\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u548c\u72b6\u6001\u7801\u3002\u8fd9\u6709\u52a9\u4e8e\u5728\u6574\u4e2a\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u5b9e\u73b0\u7edf\u4e00\u7684\u5168\u5c40\u5f02\u5e38\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Cloud \u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\u901a\u5e38\u662f\u901a\u8fc7\u4f7f\u7528 Spring Boot \u63d0\u4f9b\u7684 @ [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[],"class_list":["post-9758","post","type-post","status-publish","format-standard","hentry","category-springcloud"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9758","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=9758"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9758\/revisions"}],"predecessor-version":[{"id":16760,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9758\/revisions\/16760"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9758"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}