SpringBoot2--错误处理

前言

本文讨论Springboot2是如何处理页面访问错误的问题

一、默认错误处理机制

1.错误处理原理

在springboot中ErrorMvcAutoConfiguration类用来处理错误,它给容器中添加了好多组件

1)ErrorPageCustomizer

系统出现错误以后来到ErrorPageCustomizer,会默认发送error请求进行处理

static class ErrorPageCustomizer implements ErrorPageRegistrar, Ordered {
   

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

	@Override
	public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
   
		ErrorPage errorPage = new ErrorPage(
				this.dispatcherServletPath.getRelativePath(this.properties.getError().getPath()));
				   	//@Value("${error.path:/error}")
                    //private String path = "/error";
		errorPageRegistry.addErrorPages(errorPage);
	}
2)BasicErrorController

用来处理/error错误请求

@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
   

    //产生html类型的数据;浏览器发送的请求来到这个方法处理
    @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);
	}

    //产生json数据,其他客户端来到这个方法处理;
	@RequestMapping
	public 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);
	}


}

在处理网页页面的过程中有个resolveErrorView方法,所有的ErrorViewResolver得到ModelAndView

protected ModelAndView resolveErrorView(HttpServletRequest request, HttpServletResponse response, HttpStatus status,
		Map<String, Object> model) {
   
	for (ErrorViewResolver resolver : this.errorViewResolvers) {
   
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值