// 默认的错误页地址是/error@Controller@RequestMapping("${server.error.path:${error.path:/error}}")public class BasicErrorController extends AbstractErrorController {@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {HttpStatus status = getStatus(request);Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));response.setStatus(status.value());ModelAndView modelAndView = resolveErrorView(request, response, status, model);return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);}@RequestMappingpublic ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {HttpStatus status = getStatus(request);if (status == HttpStatus.NO_CONTENT) {return new ResponseEntity<>(status);}Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));return new ResponseEntity<>(body, status);}}
这里有两个方法,分别处理了不同的Accept请求头 。到此你是否真正地明白了Springboot中的错误处理的工作原理呢?
推荐阅读
- SpringBoot通过一个注解结合Redis实现接口限流就是这么简单
- MySQL运行时的可观测性
- 如何通过数据层的现代化来消解数字化转型的四个误区
- Dynalang——一种使用语言学习世界模型的AI新技术
- 快速、可靠的JavaScript依赖管理工具——yarn
- 如今电脑重装系统的方法都有哪些
- Django 入门:构建Python Web应用的全面指南
- 各省份公务员待遇流出,工资不是重点公积金亮了,是心动的感觉
- 啤酒的用处生活小技巧 啤酒的用处
- 青芒要放黄了才能吃吗 硬邦邦的青芒果怎么吃