{"id":125,"date":"2026-07-23T23:40:50","date_gmt":"2026-07-23T15:40:50","guid":{"rendered":"https:\/\/fsdata.site\/?p=125"},"modified":"2026-07-23T23:40:50","modified_gmt":"2026-07-23T15:40:50","slug":"springboot-global-exception-handler","status":"publish","type":"post","link":"https:\/\/fsdata.site\/?p=125","title":{"rendered":"Spring Boot \u7edf\u4e00\u5f02\u5e38\u5904\u7406\u4e0e\u7edf\u4e00\u8fd4\u56de\u5c01\u88c5"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u9700\u8981\u7edf\u4e00\u5f02\u5e38\u5904\u7406<\/h2>\n<p>\u6ca1\u6709\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u65f6\uff0cController \u91cc\u5230\u5904\u662f try-catch\uff0c\u524d\u7aef\u62ff\u5230\u7684\u9519\u8bef\u683c\u5f0f\u4e94\u82b1\u516b\u95e8\uff1a\u6709\u7684\u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u6709\u7684\u8fd4\u56de 500 \u88f8\u5f02\u5e38\uff0c\u6709\u7684\u628a\u5806\u6808\u76f4\u63a5\u629b\u7ed9\u7528\u6237\u3002Spring Boot \u7528 <code>@RestControllerAdvice<\/code> + <code>@ExceptionHandler<\/code> \u53ef\u4ee5\u628a\u5f02\u5e38\u5728\u4e00\u5904\u6536\u53e3\uff0c\u518d\u914d\u5408\u7edf\u4e00\u8fd4\u56de\u4f53\uff0c\u524d\u540e\u7aef\u5bf9\u63a5\u4f1a\u6e05\u723d\u5f88\u591a\u3002<\/p>\n<h2>\u4e00\u3001\u5148\u5b9a\u4e49\u7edf\u4e00\u8fd4\u56de\u7ed3\u6784<\/h2>\n<pre><code>public class Result&lt;T&gt; {\n    private int code;\n    private String msg;\n    private T data;\n\n    public static &lt;T&gt; Result&lt;T&gt; ok(T data) {\n        Result&lt;T&gt; r = new Result&lt;&gt;();\n        r.code = 0; r.msg = \"success\"; r.data = data;\n        return r;\n    }\n    public static Result&lt;?&gt; fail(int code, String msg) {\n        Result&lt;?&gt; r = new Result&lt;&gt;();\n        r.code = code; r.msg = msg;\n        return r;\n    }\n    \/\/ getter\/setter \u7701\u7565\n}\n<\/code><\/pre>\n<h2>\u4e8c\u3001\u5168\u5c40\u5f02\u5e38\u5904\u7406\u5668<\/h2>\n<pre><code>@RestControllerAdvice\npublic class GlobalExceptionHandler {\n\n    \/\/ \u4e1a\u52a1\u5f02\u5e38\n    @ExceptionHandler(BusinessException.class)\n    public Result&lt;?&gt; handleBiz(BusinessException e) {\n        return Result.fail(e.getCode(), e.getMessage());\n    }\n\n    \/\/ \u53c2\u6570\u6821\u9a8c\u5931\u8d25\n    @ExceptionHandler(MethodArgumentNotValidException.class)\n    public Result&lt;?&gt; handleValid(MethodArgumentNotValidException e) {\n        String msg = e.getBindingResult().getFieldError().getDefaultMessage();\n        return Result.fail(400, msg);\n    }\n\n    \/\/ \u515c\u5e95\uff1a\u672a\u77e5\u5f02\u5e38\n    @ExceptionHandler(Exception.class)\n    public Result&lt;?&gt; handleOther(Exception e) {\n        \/\/ \u751f\u4ea7\u73af\u5883\u4e0d\u8981\u66b4\u9732\u5806\u6808\n        return Result.fail(500, \"\u7cfb\u7edf\u5f02\u5e38\uff0c\u8bf7\u7a0d\u540e\u91cd\u8bd5\");\n    }\n}\n<\/code><\/pre>\n<h2>\u4e09\u3001\u4e1a\u52a1\u5f02\u5e38\u4e0e\u6821\u9a8c<\/h2>\n<p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u643a\u5e26\u4e1a\u52a1\u7801\uff1a<\/p>\n<pre><code>public class BusinessException extends RuntimeException {\n    private int code;\n    public BusinessException(int code, String msg) {\n        super(msg); this.code = code;\n    }\n    \/\/ getter\n}\n<\/code><\/pre>\n<p>Controller \u91cc\u76f4\u63a5\u629b\uff0c\u4e0d\u7528\u81ea\u5df1\u5305\uff1a<\/p>\n<pre><code>@PostMapping(\"\/order\")\npublic Result&lt;Order&gt; create(@Valid @RequestBody CreateOrderReq req) {\n    if (req.getCount() &lt;= 0)\n        throw new BusinessException(1001, \"\u8d2d\u4e70\u6570\u91cf\u5fc5\u987b\u5927\u4e8e0\");\n    return Result.ok(orderService.create(req));\n}\n<\/code><\/pre>\n<p><code>@Valid<\/code> \u89e6\u53d1 Bean Validation\uff0c\u6821\u9a8c\u5931\u8d25\u81ea\u52a8\u8fdb <code>MethodArgumentNotValidException<\/code>\uff0c\u8fd4\u56de\u5b57\u6bb5\u7ea7\u7684\u53cb\u597d\u63d0\u793a\u3002<\/p>\n<h2>\u56db\u3001\u51e0\u4e2a\u6ce8\u610f\u70b9<\/h2>\n<ul>\n<li><strong>\u751f\u4ea7\u73af\u5883\u522b\u628a\u5f02\u5e38\u5806\u6808\u8fd4\u56de\u524d\u7aef<\/strong>\uff0c\u7edf\u4e00\u8fd4\u56de&#8221;\u7cfb\u7edf\u5f02\u5e38&#8221;\u5373\u53ef\uff0c\u7ec6\u8282\u8bb0\u65e5\u5fd7\u3002<\/li>\n<li>\u5168\u5c40\u5904\u7406\u5668\u5efa\u8bae\u653e\u5728\u72ec\u7acb\u7684 package\uff0c\u786e\u4fdd\u88ab Spring \u626b\u63cf\u5230\u3002<\/li>\n<li>\u5982\u679c\u7528\u4e86 <code>Filter<\/code>\/<code>Interceptor<\/code> \u91cc\u629b\u7684\u5f02\u5e38\uff0c<code>@RestControllerAdvice<\/code> \u62e6\u4e0d\u5230\uff0c\u9700\u8981\u5728 Filter \u5c42\u5355\u72ec\u5904\u7406\u3002<\/li>\n<\/ul>\n<h2>\u5c0f\u7ed3<\/h2>\n<p>\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u7684\u6838\u5fc3\u5c31\u4e00\u53e5\u8bdd\uff1a<strong>\u5f02\u5e38\u5728\u4e00\u5904\u6536\u53e3\uff0c\u8fd4\u56de\u683c\u5f0f\u53ea\u6709\u4e00\u79cd<\/strong>\u3002\u914d\u5408 <code>Result&lt;T&gt;<\/code> \u548c\u5206\u5c42\u5f02\u5e38\uff0c\u524d\u7aef\u518d\u4e5f\u4e0d\u7528\u5199\u4e00\u5806 if \u5224\u65ad\u9519\u8bef\u7ed3\u6784\uff0c\u8054\u8c03\u6548\u7387\u76f4\u63a5\u7ffb\u500d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 @RestControllerAdvice + \u7edf\u4e00 Result \u4f53\uff0c\u628a\u5f02\u5e38\u548c\u8fd4\u56de\u7ed3\u6784\u4e00\u6b21\u6027\u6536\u62e2\uff0c\u524d\u540e\u7aef\u5bf9\u63a5\u66f4\u7701\u5fc3\u3002<\/p>\n","protected":false},"author":2,"featured_media":114,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-dev"],"_links":{"self":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=125"}],"version-history":[{"count":0,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/125\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/media\/114"}],"wp:attachment":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}