{"id":217784,"date":"2021-05-18T08:41:53","date_gmt":"2021-05-18T00:41:53","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=217784"},"modified":"2021-05-04T10:21:34","modified_gmt":"2021-05-04T02:21:34","slug":"redirection-and-pipeline","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/redirection-and-pipeline.html","title":{"rendered":"Spring Boot\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u7684\u62e6\u622a\u6307\u5357"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u901a\u5e38\u6211\u4eec\u5728Spring Boot\u4e2d\u8bbe\u7f6e\u7684\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u53ea\u80fd\u5904\u7406Controller\u629b\u51fa\u7684\u5f02\u5e38\u3002\u6709\u4e9b\u8bf7\u6c42\u8fd8\u6ca1\u5230Controller\u5c31\u51fa\u5f02\u5e38\u4e86\uff0c\u800c\u8fd9\u4e9b\u5f02\u5e38\u4e0d\u80fd\u88ab\u7edf\u4e00\u5f02\u5e38\u6355\u83b7\uff0c\u4f8b\u5982Servlet\u5bb9\u5668\u7684\u67d0\u4e9b\u5f02\u5e38\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u901a\u5e38\u6211\u4eec\u5728Spring Boot\u4e2d\u8bbe\u7f6e\u7684\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u53ea\u80fd\u5904\u7406Controller\u629b\u51fa\u7684\u5f02\u5e38\u3002\u6709\u4e9b\u8bf7\u6c42\u8fd8\u6ca1\u5230Controller\u5c31\u51fa\u5f02\u5e38\u4e86\uff0c\u800c\u8fd9\u4e9b\u5f02\u5e38\u4e0d\u80fd\u88ab\u7edf\u4e00\u5f02\u5e38\u6355\u83b7\uff0c\u4f8b\u5982Servlet\u5bb9\u5668\u7684\u67d0\u4e9b\u5f02\u5e38\u3002\u4eca\u5929\u6211\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\u5c31\u9047\u5230\u4e86\u4e00\u4e2a\uff0c\u8fd9\u8ba9\u6211\u5f88\u4e0d\u723d\uff0c\u56e0\u4e3a\u5b83\u8fd4\u56de\u7684\u9519\u8bef\u4fe1\u606f\u683c\u5f0f\u4e0d\u80fd\u7edf\u4e00\u5904\u7406\uff0c\u6211\u51b3\u5b9a\u627e\u4e2a\u65b9\u6848\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n

ErrorPageFilter<\/strong><\/div>\n

\"\"
\nWhitelabel Error Page<\/p>\n

\u8fd9\u7c7b\u56fe\u76f8\u4fe1\u5927\u5bb6\u6ca1\u5c11\u89c1\uff0cSpring Boot \u53ea\u8981\u51fa\u9519\uff0c\u4f53\u73b0\u5728\u9875\u9762\u4e0a\u7684\u5c31\u662f\u8fd9\u4e2a\u3002\u5982\u679c\u4f60\u7528Postman\u4e4b\u7c7b\u7684\u6d4b\u8bd5\u51fa\u4e86\u5f02\u5e38\u5219\u662f\uff1a<\/p>\n

\r\n{ \r\n  \"timestamp\": \"2021-04-29T22:45:33.231+0000\", \r\n  \"status\": 500, \r\n  \"message\": \"Internal Server Error\", \r\n  \"path\": \"foo\/bar\" \r\n} \r\n<\/pre>\n

\u8fd9\u4e2a\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u5462?Spring Boot\u5728\u542f\u52a8\u65f6\u4f1a\u6ce8\u518c\u4e00\u4e2aErrorPageFilter\uff0c\u5f53Servlet\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u8be5\u8fc7\u6ee4\u5668\u5c31\u4f1a\u62e6\u622a\u5904\u7406\uff0c\u5c06\u5f02\u5e38\u6839\u636e\u4e0d\u540c\u7684\u7b56\u7565\u8fdb\u884c\u5904\u7406\uff1a\u5f53\u5f02\u5e38\u5df2\u7ecf\u5728\u5904\u7406\u7684\u8bdd\u76f4\u63a5\u5904\u7406\uff0c\u5426\u5219\u8f6c\u53d1\u7ed9\u5bf9\u5e94\u7684\u9519\u8bef\u9875\u9762\u3002\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u53bb\u770b\u4e0b\u6e90\u7801\uff0c\u903b\u8f91\u4e0d\u590d\u6742\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8d34\u4e86\u3002<\/p>\n

\u53e6\u5916\u5f53\u4e00\u4e2a Servlet \u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u65f6\uff0c\u5904\u7406\u5f02\u5e38\u7684Servlet\u53ef\u4ee5\u4eceHttpServletRequest\u91cc\u9762\u5f97\u5230\u51e0\u4e2a\u5c5e\u6027\uff0c\u5982\u4e0b\uff1a
\n\"\"
\n\u5f02\u5e38\u5c5e\u6027<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u4ece\u4e0a\u9762\u7684\u51e0\u4e2a\u5c5e\u6027\u4e2d\u83b7\u53d6\u5f02\u5e38\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n

\u9ed8\u8ba4\u9519\u8bef\u9875\u9762<\/strong><\/div>\n

\u901a\u5e38Spring Boot\u51fa\u73b0\u5f02\u5e38\u9ed8\u8ba4\u4f1a\u8df3\u8f6c\u5230\/error\u8fdb\u884c\u5904\u7406\uff0c\u800c\/error\u7684\u76f8\u5173\u903b\u8f91\u5219\u662f\u7531BasicErrorController\u5b9e\u73b0\u7684\u3002<\/p>\n

\r\n@Controller \r\n@RequestMapping(\"${server.error.path:${error.path:\/error}}\") \r\npublic class BasicErrorController extends AbstractErrorController { \r\n    \/\/\u8fd4\u56de\u9519\u8bef\u9875\u9762 \r\n  @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) \r\n public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { \r\n  HttpStatus status = getStatus(request); \r\n  Map model = Collections \r\n    .unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML))); \r\n  response.setStatus(status.value()); \r\n  ModelAndView modelAndView = resolveErrorView(request, response, status, model); \r\n  return (modelAndView != null) ? modelAndView : new ModelAndView(\"error\", model); \r\n } \r\n    \/\/ \u8fd4\u56dejson \r\n @RequestMapping \r\n public ResponseEntity> error(HttpServletRequest request) { \r\n  HttpStatus status = getStatus(request); \r\n  if (status == HttpStatus.NO_CONTENT) { \r\n   return new ResponseEntity<>(status); \r\n  } \r\n  Map body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL)); \r\n  return new ResponseEntity<>(body, status); \r\n }   \r\n\/\/ \u5176\u5b83\u7701\u7565 \r\n} \r\n<\/pre>\n

\u800c\u5bf9\u5e94\u7684\u914d\u7f6e\uff1a<\/p>\n

\r\n@Bean \r\n@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT) \r\npublic BasicErrorController basicErrorController(ErrorAttributes errorAttributes, \r\n      ObjectProvider errorViewResolvers) { \r\n   return new BasicErrorController(errorAttributes, this.serverProperties.getError(), \r\n         errorViewResolvers.orderedStream().collect(Collectors.toList())); \r\n} \r\n<\/errorviewresolver><\/pre>\n

\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u91cd\u65b0\u5b9e\u73b0\u4e00\u4e2aErrorController\u5e76\u6ce8\u5165Spring IoC\u5c31\u53ef\u4ee5\u66ff\u4ee3\u9ed8\u8ba4\u7684\u5904\u7406\u673a\u5236\u3002\u800c\u4e14\u6211\u4eec\u53ef\u4ee5\u5f88\u6e05\u6670\u7684\u53d1\u73b0\u8fd9\u4e2aBasicErrorController\u4e0d\u4f46\u662fErrorController\u7684\u5b9e\u73b0\u800c\u4e14\u662f\u4e00\u4e2a\u63a7\u5236\u5668\uff0c\u5982\u679c\u6211\u4eec\u8ba9\u63a7\u5236\u5668\u7684\u65b9\u6cd5\u629b\u5f02\u5e38\uff0c\u80af\u5b9a\u53ef\u4ee5\u88ab\u81ea\u5b9a\u4e49\u7684\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u3002\u6240\u4ee5\u6211\u5bf9BasicErrorController\u8fdb\u884c\u4e86\u6539\u9020:<\/p>\n

\r\n@Controller \r\n@RequestMapping(\"${server.error.path:${error.path:\/error}}\") \r\npublic class ExceptionController extends AbstractErrorController { \r\n \r\n \r\n    public ExceptionController(ErrorAttributes errorAttributes) { \r\n        super(errorAttributes); \r\n    } \r\n \r\n \r\n    @Override \r\n    @Deprecated \r\n    public String getErrorPath() { \r\n        return null; \r\n    } \r\n \r\n    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) \r\n    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { \r\n        throw new RuntimeException(getErrorMessage(request)); \r\n    } \r\n \r\n    @RequestMapping \r\n    public ResponseEntity> error(HttpServletRequest request) { \r\n        throw new RuntimeException(getErrorMessage(request)); \r\n    } \r\n \r\n    private String getErrorMessage(HttpServletRequest request) { \r\n        Object code = request.getAttribute(\"javax.servlet.error.status_code\"); \r\n        Object exceptionType = request.getAttribute(\"javax.servlet.error.exception_type\"); \r\n        Object message = request.getAttribute(\"javax.servlet.error.message\"); \r\n        Object path = request.getAttribute(\"javax.servlet.error.request_uri\"); \r\n        Object exception = request.getAttribute(\"javax.servlet.error.exception\"); \r\n \r\n        return String.format(\"code: %s,exceptionType: %s,message: %s,path: %s,exception: %s\", \r\n                code, exceptionType, message, path, exception); \r\n    } \r\n} \r\n<\/pre>\n

\u76f4\u63a5\u629b\u5f02\u5e38\uff0c\u7b80\u5355\u7701\u529b!\u51e1\u662f\u8fd9\u91cc\u6355\u6349\u7684\u5230\u7684\u5f02\u5e38\u5927\u90e8\u5206\u8fd8\u6ca1\u6709\u7ecf\u8fc7Controller\uff0c\u6211\u4eec\u901a\u8fc7ExceptionController\u4e2d\u7ee7\u4e5f\u8ba9\u8fd9\u4e9b\u5f02\u5e38\u88ab\u7edf\u4e00\u5904\u7406\uff0c\u4fdd\u8bc1\u6574\u4e2a\u5e94\u7528\u7684\u5f02\u5e38\u5904\u7406\u5bf9\u5916\u4fdd\u6301\u4e00\u4e2a\u7edf\u4e00\u7684\u95e8\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u901a\u5e38\u6211\u4eec\u5728Spring Boot\u4e2d\u8bbe\u7f6e\u7684\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u53ea\u80fd\u5904\u7406Controller\u629b\u51fa\u7684\u5f02\u5e38\u3002\u6709\u4e9b\u8bf7\u6c42\u8fd8\u6ca1\u5230Co […]<\/p>\n","protected":false},"author":1898,"featured_media":217790,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[684],"class_list":["post-217784","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-spring-boot"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/217784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=217784"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/217784\/revisions"}],"predecessor-version":[{"id":217791,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/217784\/revisions\/217791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/217790"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=217784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=217784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=217784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}