springboot mvc 处理异常

来源:互联网 发布:网络安全法与等级保护 编辑:程序博客网 时间:2024/06/06 05:15

在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作。下图列出了Spring Boot中跟MVC异常处理相关的类。

Spring Boot在启动过程中会根据当前环境进行AutoConfiguration,其中跟MVC错误处理相关的配置内容,在ErrorMvcAutoConfiguration这个类中。以下会分块介绍这个类里面的配置。

在Servlet容器中添加了一个默认的错误页面

因为ErrorMvcAutoConfiguration类实现了EmbeddedServletContainerCustomizer接口,所以可以通过override customize方法来定制Servlet容器。以下代码摘自ErrorMvcAutoConfiguration:

@Value("${error.path:/error}")private String errorPath = "/error";@Overridepublic void customize(ConfigurableEmbeddedServletContainer container) {    container.addErrorPages(new ErrorPage(this.properties.getServletPrefix()        + this.errorPath));}

可以看到ErrorMvcAutoConfiguration在容器中,添加了一个错误页面/error。因为这项配置的存在,如果Spring MVC在处理过程抛出异常到Servlet容器,容器会定向到这个错误页面/error。

那么我们有什么可以配置的呢?

  1. 我们可以配置错误页面的url,/error是默认值,我们可以再application.properties中通过设置error.path的值来配置该页面的url;
  2. 我们可以提供一个自定义的EmbeddedServletContainerCustomizer,添加更多的错误页面,比如对不同的http status code,使用不同的错误处理页面。就像下面这段代码一样:
@Beanpublic EmbeddedServletContainerCustomizer containerCustomizer() {    return new EmbeddedServletContainerCustomizer() {        @Override        public void customize(ConfigurableEmbeddedServletContainer container) {            container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));            container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"));        }    };}

定义了ErrorAttributes接口,并默认配置了一个DefaultErrorAttributes Bean

以下代码摘自ErrorMvcAutoConfiguration:

@Bean@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)public DefaultErrorAttributes errorAttributes() {    return new DefaultErrorAttributes();}

以下代码摘自DefaultErrorAttributes, ErrorAttributes, HandlerExceptionResolver:

@Order(Ordered.HIGHEST_PRECEDENCE)public class DefaultErrorAttributes implements ErrorAttributes, HandlerExceptionResolver,    Ordered {    //篇幅原因,忽略类的实现代码。}public interface ErrorAttributes {    public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,        boolean includeStackTrace);    public Throwable getError(RequestAttributes requestAttributes);}public interface HandlerExceptionResolver {    ModelAndView resolveException(        HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);}

这个DefaultErrorAttributes有什么用呢?主要有两个作用:

  1. 实现了ErrorAttributes接口,具备提供Error Attributes的能力,当处理/error错误页面时,需要从该bean中读取错误信息以供返回;
  2. 实现了HandlerExceptionResolver接口并具有最高优先级,即DispatcherServlet在doDispatch过程中有异常抛出时,先由DefaultErrorAttributes处理。从下面代码中可以发现,DefaultErrorAttributes在处理过程中,是讲ErrorAttributes保存到了request中。事实上,这是DefaultErrorAttributes能够在后面返回Error Attributes的原因,实现HandlerExceptionResolver接口,是DefaultErrorAttributes实现ErrorAttributes接口的手段。
@Overridepublic ModelAndView resolveException(HttpServletRequest request,    HttpServletResponse response, Object handler, Exception ex) {    storeErrorAttributes(request, ex);    return null;}

我们有什么可以配置的呢?
1、我们可以继承DefaultErrorAttributes,修改Error Attributes,比如下面这段代码,去掉了默认存在的error和exception这两个字段,以隐藏敏感信息。

@Beanpublic DefaultErrorAttributes errorAttributes() {    return new DefaultErrorAttributes() {        @Override        public Map<String, Object> getErrorAttributes (RequestAttributes requestAttributes,        boolean includeStackTrace){            Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);            errorAttributes.remove("error");            errorAttributes.remove("exception");            return errorAttributes;        }    };}
  1. 我们可以自己实现ErrorAttributes接口,实现自己的Error Attributes方案, 只要配置一个类型为ErrorAttributes的bean,默认的DefaultErrorAttributes就不会被配置。

提供并配置了ErrorController和ErrorView

ErrorController和ErrorView提供了对错误页面/error的支持。ErrorMvcAutoConfiguration默认配置了BasicErrorController和WhiteLabelErrorView,以下代码摘自ErrorMvcAutoConfiguration:

@Bean@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {    return new BasicErrorController(errorAttributes);}@Configuration@ConditionalOnProperty(prefix = "error.whitelabel", name = "enabled", matchIfMissing = true)@Conditional(ErrorTemplateMissingCondition.class)protected static class WhitelabelErrorViewConfiguration {    private final SpelView defaultErrorView = new SpelView(            "<html><body><h1>Whitelabel Error Page</h1>"                    + "<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>"                    + "<div id='created'>${timestamp}</div>"                    + "<div>There was an unexpected error (type=${error}, status=${status}).</div>"                    + "<div>${message}</div></body></html>");    @Bean(name = "error")    @ConditionalOnMissingBean(name = "error")    public View defaultErrorView() {        return this.defaultErrorView;    }    // If the user adds @EnableWebMvc then the bean name view resolver from    // WebMvcAutoConfiguration disappears, so add it back in to avoid disappointment.    @Bean    @ConditionalOnMissingBean(BeanNameViewResolver.class)    public BeanNameViewResolver beanNameViewResolver() {        BeanNameViewResolver resolver = new BeanNameViewResolver();        resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);        return resolver;    }}

ErrorController根据Accept头的内容,输出不同格式的错误响应。比如针对浏览器的请求生成html页面,针对其它请求生成json格式的返回。代码如下:

@RequestMapping(value = "${error.path:/error}", produces = "text/html")public ModelAndView errorHtml(HttpServletRequest request) {    return new ModelAndView("error", getErrorAttributes(request, false));}@RequestMapping(value = "${error.path:/error}")@ResponseBodypublic ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {    Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));    HttpStatus status = getStatus(request);    return new ResponseEntity<Map<String, Object>>(body, status);}

WhitelabelErrorView则提供了一个默认的白板错误页面。

我们有什么可以配置的呢?

  1. 我们可以提供自己的名字为error的view,以替换掉默认的白板页面,提供自己想要的样式。
  2. 我们可以继承BasicErrorController或者干脆自己实现ErrorController接口,用来响应/error这个错误页面请求,可以提供更多类型的错误格式等。




实例  :

重写定义错误页面的url,默认只有一个/error

@Bean    public EmbeddedServletContainerCustomizer containerCustomizer(){        return new EmbeddedServletContainerCustomizer(){            @Override            public void customize(ConfigurableEmbeddedServletContainer container) {                container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));                container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));                container.addErrorPages(new ErrorPage(java.lang.Throwable.class,"/error/500"));            }        };    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

重写通过实现ErrorController,重写BasicErrorController的功能实现

** * 重写BasicErrorController,主要负责系统的异常页面的处理以及错误信息的显示 * @see org.springframework.boot.autoconfigure.web.BasicErrorController * @see org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration * * @author Jonathan * @version 2016/5/31 11:22 * @since JDK 7.0+ */@Controller@RequestMapping(value = "error")@EnableConfigurationProperties({ServerProperties.class})public class ExceptionController implements ErrorController {    private ErrorAttributes errorAttributes;    @Autowired    private ServerProperties serverProperties;    /**     * 初始化ExceptionController     * @param errorAttributes     */    @Autowired    public ExceptionController(ErrorAttributes errorAttributes) {        Assert.notNull(errorAttributes, "ErrorAttributes must not be null");        this.errorAttributes = errorAttributes;    }    /**     * 定义404的ModelAndView     * @param request     * @param response     * @return     */    @RequestMapping(produces = "text/html",value = "404")    public ModelAndView errorHtml404(HttpServletRequest request,                                  HttpServletResponse response) {        response.setStatus(getStatus(request).value());        Map<String, Object> model = getErrorAttributes(request,                isIncludeStackTrace(request, MediaType.TEXT_HTML));        return new ModelAndView("error/404", model);    }    /**     * 定义404的JSON数据     * @param request     * @return     */    @RequestMapping(value = "404")    @ResponseBody    public ResponseEntity<Map<String, Object>> error404(HttpServletRequest request) {        Map<String, Object> body = getErrorAttributes(request,                isIncludeStackTrace(request, MediaType.TEXT_HTML));        HttpStatus status = getStatus(request);        return new ResponseEntity<Map<String, Object>>(body, status);    }    /**     * 定义500的ModelAndView     * @param request     * @param response     * @return     */    @RequestMapping(produces = "text/html",value = "500")    public ModelAndView errorHtml500(HttpServletRequest request,                                  HttpServletResponse response) {        response.setStatus(getStatus(request).value());        Map<String, Object> model = getErrorAttributes(request,                isIncludeStackTrace(request, MediaType.TEXT_HTML));        return new ModelAndView("error/500", model);    }    /**     * 定义500的错误JSON信息     * @param request     * @return     */    @RequestMapping(value = "500")    @ResponseBody    public ResponseEntity<Map<String, Object>> error500(HttpServletRequest request) {        Map<String, Object> body = getErrorAttributes(request,                isIncludeStackTrace(request, MediaType.TEXT_HTML));        HttpStatus status = getStatus(request);        return new ResponseEntity<Map<String, Object>>(body, status);    }    /**     * Determine if the stacktrace attribute should be included.     * @param request the source request     * @param produces the media type produced (or {@code MediaType.ALL})     * @return if the stacktrace attribute should be included     */    protected boolean isIncludeStackTrace(HttpServletRequest request,                                          MediaType produces) {        ErrorProperties.IncludeStacktrace include = this.serverProperties.getError().getIncludeStacktrace();        if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {            return true;        }        if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {            return getTraceParameter(request);        }        return false;    }    /**     * 获取错误的信息     * @param request     * @param includeStackTrace     * @return     */    private Map<String, Object> getErrorAttributes(HttpServletRequest request,                                                   boolean includeStackTrace) {        RequestAttributes requestAttributes = new ServletRequestAttributes(request);        return this.errorAttributes.getErrorAttributes(requestAttributes,                includeStackTrace);    }    /**     * 是否包含trace     * @param request     * @return     */    private boolean getTraceParameter(HttpServletRequest request) {        String parameter = request.getParameter("trace");        if (parameter == null) {            return false;        }        return !"false".equals(parameter.toLowerCase());    }    /**     * 获取错误编码     * @param request     * @return     */    private HttpStatus getStatus(HttpServletRequest request) {        Integer statusCode = (Integer) request                .getAttribute("javax.servlet.error.status_code");        if (statusCode == null) {            return HttpStatus.INTERNAL_SERVER_ERROR;        }        try {            return HttpStatus.valueOf(statusCode);        }        catch (Exception ex) {            return HttpStatus.INTERNAL_SERVER_ERROR;        }    }    /**     * 实现错误路径,暂时无用     * @see ExceptionMvcAutoConfiguration#containerCustomizer()     * @return     */    @Override    public String getErrorPath() {        return "";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166

总结

第一步,通过定义containerCustomizer,重写定义了异常处理对应的视图。目前定义了404和500,可以继续扩展。 
第二步,重写BasicErrorController,当然可以直接定义一个普通的controller类,直接实现第一步定义的视图的方法。重写的目的是重用ErrorAttributes。这样在页面,直接可以获取到status,message,exception,trace等内容。 
如果仅仅是把异常处理的视图作为静态页面,不需要看到异常信息内容的话,直接第一步后,再定义error/404,error/500等静态视图即可。

ErrorController根据Accept头的内容,输出不同格式的错误响应。比如针对浏览器的请求生成html页面,针对其它请求生成json格式的返回

以上两步的操作,比网上流传的更能实现自定义化。






0 0