spring boot 异常处理

阳光穿透心脏的1/2处 2022-08-21 12:48 114阅读 0赞

spring boot在异常的处理中,默认实现了一个EmbeddedServletContainerCustomizer并定义了一个错误页面到”/error”中,在ErrorMvcAutoConfiguration源码中可以看到

  1. /** * {@link EmbeddedServletContainerCustomizer} that configures the container's error * pages. */
  2. private static class ErrorPageCustomizer implements EmbeddedServletContainerCustomizer, Ordered {
  3. private final ServerProperties properties;
  4. protected ErrorPageCustomizer(ServerProperties properties) {
  5. this.properties = properties;
  6. }
  7. @Override
  8. public void customize(ConfigurableEmbeddedServletContainer container) {
  9. container.addErrorPages(new ErrorPage(this.properties.getServletPrefix()
  10. + this.properties.getError().getPath()));
  11. }
  12. @Override
  13. public int getOrder() {
  14. return 0;
  15. }
  16. }

还配置了一个默认的白板页面,在源码可以看到

  1. @Configuration
  2. @ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)
  3. @Conditional(ErrorTemplateMissingCondition.class)
  4. protected static class WhitelabelErrorViewConfiguration {
  5. private final SpelView defaultErrorView = new SpelView(
  6. "<html><body><h1>Whitelabel Error Page</h1>"
  7. + "<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>"
  8. + "<div id='created'>${timestamp}</div>"
  9. + "<div>There was an unexpected error (type=${error}, status=${status}).</div>"
  10. + "<div>${message}</div></body></html>");
  11. @Bean(name = "error")
  12. @ConditionalOnMissingBean(name = "error")
  13. public View defaultErrorView() {
  14. return this.defaultErrorView;
  15. }
  16. // If the user adds @EnableWebMvc then the bean name view resolver from
  17. // WebMvcAutoConfiguration disappears, so add it back in to avoid disappointment.
  18. @Bean
  19. @ConditionalOnMissingBean(BeanNameViewResolver.class)
  20. public BeanNameViewResolver beanNameViewResolver() {
  21. BeanNameViewResolver resolver = new BeanNameViewResolver();
  22. resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
  23. return resolver;
  24. }
  25. }

在路径的处理上,定义了一个BasicErrorController来处理异常,ErrorAttributes来装载错误异常到前端

  1. @Controller
  2. @RequestMapping("${server.error.path:${error.path:/error}}")
  3. public class BasicErrorController extends AbstractErrorController {
  4. private final ErrorProperties errorProperties;
  5. //...
  6. @RequestMapping(produces = "text/html")
  7. public ModelAndView errorHtml(HttpServletRequest request,
  8. HttpServletResponse response) {
  9. response.setStatus(getStatus(request).value());
  10. Map<String, Object> model = getErrorAttributes(request,
  11. isIncludeStackTrace(request, MediaType.TEXT_HTML));
  12. return new ModelAndView("error", model);
  13. }
  14. @RequestMapping
  15. @ResponseBody
  16. public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
  17. Map<String, Object> body = getErrorAttributes(request,
  18. isIncludeStackTrace(request, MediaType.ALL));
  19. HttpStatus status = getStatus(request);
  20. return new ResponseEntity<Map<String, Object>>(body, status);
  21. }
  22. //...
  23. }

默认只要你请求的content-type是”text/html”则返回一个白版页面给你,如果是其他的content-type,则返回一个json的数据给你。

若你想自定义异常,可以通过以下方式来处理

1.你可以默认在classpath:templates下定义一个error.ftl的freemarker模版来定义异常页面

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <h1> error page !! </h1>
  9. ${timestamp?string('yyyy-MM-dd HH:mm:ss')}<br/><br/>
  10. ${status} <br/><br/>
  11. ${error} <br/><br/>
  12. ${message} <br/><br/>
  13. ${path} <br/><br/>
  14. </body>
  15. </html>

2.可以利用@ExceptionHandler来为某个特定的controller拦截异常,并返回一个json数据,当然你也可以定义全部的controller来拦截异常

  1. @ControllerAdvice(basePackageClasses = FooController.class)
  2. public class FooControllerAdvice extends ResponseEntityExceptionHandler {
  3. @ExceptionHandler(YourException.class)
  4. @ResponseBody
  5. ResponseEntity<?> handleControllerException(HttpServletRequest request, Throwable ex) {
  6. HttpStatus status = getStatus(request);
  7. return new ResponseEntity<>(new CustomErrorType(status.value(), ex.getMessage()), status);
  8. }
  9. private HttpStatus getStatus(HttpServletRequest request) {
  10. Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
  11. if (statusCode == null) {
  12. return HttpStatus.INTERNAL_SERVER_ERROR;
  13. }
  14. return HttpStatus.valueOf(statusCode);
  15. }
  16. }

3.还可以自定义server的错误页面

  1. @Bean
  2. public EmbeddedServletContainerCustomizer containerCustomizer(){
  3. return new MyCustomizer();
  4. }
  5. // ...
  6. private static class MyCustomizer implements EmbeddedServletContainerCustomizer {
  7. @Override
  8. public void customize(ConfigurableEmbeddedServletContainer container) {
  9. container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/500"));
  10. }
  11. }

发表评论

表情:
评论列表 (有 0 条评论,114人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot全局异常处理

    在开发Web应用程序时,异常处理是非常重要的。在Spring Boot中,我们可以使用全局异常处理器来捕获并处理应用程序中的异常。本文将介绍如何使用Spring Boot的全局

    相关 spring boot异常处理

    目的:异常处理采用的是枚举型的  ExceptionEnum 枚举类  (特点 和别的公司的不太一样) 我们通常会把 事务 配置在 Service层,当数据库操作失败时让 S