{"id":920,"date":"2023-03-11T17:13:03","date_gmt":"2023-03-11T09:13:03","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=920"},"modified":"2023-04-29T15:48:35","modified_gmt":"2023-04-29T07:48:35","slug":"spring-boot-implement-errorcontroller-interface-to-handle-error-pages-such-as-404-and-500","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/spring-boot-implement-errorcontroller-interface-to-handle-error-pages-such-as-404-and-500\/","title":{"rendered":"Spring Boot \u5b9e\u73b0ErrorController\u63a5\u53e3\u5904\u7406404\u3001500\u7b49\u9519\u8bef\u9875\u9762"},"content":{"rendered":"<p>\u5728\u9879\u76ee\u4e2d\u6211\u4eec\u9047\u5230404\u627e\u4e0d\u5230\u7684\u9519\u8bef\u3001\u6216\u8005500\u670d\u52a1\u5668\u9519\u8bef\u90fd\u9700\u8981\u914d\u7f6e\u76f8\u5e94\u7684\u9875\u9762\u7ed9\u7528\u6237\u4e00\u4e2a\u53cb\u597d\u7684\u63d0\u793a\uff0c\u800c\u5728Spring Boot\u4e2d\u6211\u4eec\u9700\u8981\u5982\u4f55\u8bbe\u7f6e\u3002<\/p>\n<p>\u6211\u4eec\u9700\u8981\u5b9e\u73b0<code>ErrorController<\/code>\u63a5\u53e3\uff0c\u91cd\u5199<code>handleError<\/code>\u65b9\u6cd5\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">import org.springframework.boot.autoconfigure.web.ErrorController\nimport org.springframework.stereotype.Controller\nimport org.springframework.web.bind.annotation.RequestMapping\n\nimport javax.servlet.http.HttpServletRequest\n\n@Controller\nclass MainsiteErrorController implements ErrorController {\n\n    @RequestMapping(&quot;\/error&quot;)\n    public String handleError(HttpServletRequest request) {\n        \/\/\u83b7\u53d6statusCode:401,404,500\n        Integer statusCode = (Integer) request.getAttribute(&quot;javax.servlet.error.status_code&quot;)\n        if (statusCode == 401) {\n            return &quot;\/401&quot;\n        } else if(statusCode == 404) {\n            return &quot;\/404&quot;\n        } else if(statusCode == 403) {\n            return &quot;\/403&quot;\n        } else {\n            return &quot;\/500&quot;\n        }\n    }\n\n    @Override\n    public String getErrorPath() {\n        return &quot;\/error&quot;\n    }\n}<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u8bbe\u7f6e\u5373\u53ef\u5b9e\u73b0\u5bf9\u5e94\u72b6\u6001\u7801\u8df3\u8f6c\u5230\u5bf9\u5e94\u7684\u63d0\u793a\u9875\u9762\u3002<\/p>\n<p>\u8fd4\u56deJSON\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code class=\"language-java\">import brave.Tracer;\nimport com.netflix.zuul.context.RequestContext;\nimport com.netflix.zuul.exception.ZuulException;\nimport org.springframework.boot.web.servlet.error.ErrorController;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport javax.annotation.Resource;\n\n@RestController\npublic class ErrorHandlerController implements ErrorController {\n\n    @Resource\n    private Tracer tracer;\n\n    @Override\n    public String getErrorPath() {\n        return null;\n    }\n\n    @RequestMapping(&quot;\/error&quot;)\n    public Object error() {\n        RequestContext ctx = RequestContext.getCurrentContext();\n        ZuulException exception = (ZuulException) ctx.getThrowable();\n        return ResultBuilder.failure(tracer.currentSpan().context().traceIdString(),\n                ResponseCode.BAD_REQUEST, &quot;invalid request&quot;);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9879\u76ee\u4e2d\u6211\u4eec\u9047\u5230404\u627e\u4e0d\u5230\u7684\u9519\u8bef\u3001\u6216\u8005500\u670d\u52a1\u5668\u9519\u8bef\u90fd\u9700\u8981\u914d\u7f6e\u76f8\u5e94\u7684\u9875\u9762\u7ed9\u7528\u6237\u4e00\u4e2a\u53cb\u597d\u7684\u63d0\u793a\uff0c\u800c\u5728Spri [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-920","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=920"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/920\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}