SpringBoot框架错误处理机制:默认错误处理机制和原理,定制错误响应页面和json数据

错误处理机制

springboot的默认错误处理机制

在浏览器上默认返回的是一个错误页面,下面所示

在这里插入图片描述

因为在浏览器中请求优先接收text/html数据

在这里插入图片描述

在其他的客户端默认返回的是json字符串
因为在其他的客户端的接收参数是: accept:"*/*"

springboot错误处理机制原理

具体的我们可以参考源码ErrorMvcAutoConfiguration,
在这个自动配置类里面主要给给容器添加了下面重要的组件:
	DefaultErrorAttributes
	BasicErrorController
	ErrorPageCustomizer
	DefaultErrorViewResolver
BasicErrorController
//可以看出也是从配置文件里面获取错误的路径,然后进行处理对应的错误请求
//如果获取不到就默认是/error
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
   
   
	//产生html类型的数据,浏览器发送的请求就来到这个方法进行处理
	@RequestMapping(produces = "text/html")
	public ModelAndView errorHtml(HttpServletRequest request,
			HttpServletResponse response) {
   
   
		HttpStatus status = getStatus(request);
		Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
				request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
		response.setStatus(status.value());
		//这个就是解析要去那个错误页面
		ModelAndView modelAndView = resolveErrorView(request, response, status, model);
		return (modelAndView == null ? new ModelAndView("error", model) : modelAndView);
	}
	
	//产生json数据,其他的客户端的请求来这个方法进行处理
	@RequestMapping
	@ResponseBody
	public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
   
   
		Map<String, Object> body = getErrorAttributes(request,
				isIncludeStackTrace(request, MediaType.ALL));
		HttpStatus status = getStatus(request);
		return new ResponseEntity<Map<String, Object>>(body, status);
	}
点进resolveErrorView进行查看,可以看出这个方法是得到所有的ErrorViewResolver
进行解析,如果解析成功就返回回去,否则就返回null
	protected ModelAndView resolveErrorView(HttpServletRequest request,
			HttpServletResponse response, HttpStatus status, Map<String, Object> model) {
   
   
		for (ErrorViewResolver resolver : this.errorViewResolvers) {
   
   
			ModelAndView modelAndView = resolver.resolveErrorView(request, status, model);
			if (modelAndView != null) {
   
   
				return modelAndView;
			}
		}
		return null;
	}
ErrorPageCustomizer
里面就是在系统出现错误之后来到error请求
里面的getPath()就是从配置文件里面获取配置的值
如果取不出,默认是/error
	@Value("${error.path:/error}")
	private String path = "/error";
	private static class ErrorPageCustomizer implements ErrorPageRegistrar, Ordered {
   
   

		private final ServerProperties properties;

		protected ErrorPageCustomizer(ServerProperties properties) {
   
   
			this.properties = properties;
		}

		@Override
		public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
   
   
			ErrorPage errorPage = new ErrorPage(this.properties.getServletPrefix()
					+ this.properties.getError().getPath());
			errorPageRegistry.addErrorPages(errorPage);
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReflectMirroring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值