• boot_自定义异常


    0.自定义异常错误页

    error/404.html error/5xx.html;有精确的错误状态码页面就匹配精确,没有就找 4xx.html;如果都没有就触发白页

    1.第一种:@ControllerAdvice+@ExceptionHandler处理全局异常

    @ControllerAdvice+@ExceptionHandler处理全局异常;底层是 ExceptionHandlerExceptionResolver 支持的
    package com.atguigu.admin.exception;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    
    /**
     * 处理整个web controll的异常
     */
    @ControllerAdvice
    @Slf4j
    public class GlobalExceptionHandler {
        @ExceptionHandler({ArithmeticException.class,NullPointerException.class}) //处理异常
        public String handleArithException(Exception e){
            log.error("异常是:{}",e);
            return "login"; //视图地址
        }
    }

    2.第二种:@ResponseStatus+自定义异常

    • @ResponseStatus+自定义异常 ;底层是 ResponseStatusExceptionResolver ,把responsestatus注解的信息底层调用 response.sendError(statusCode, resolvedReason);tomcat发送的/error
    • Spring底层的异常,如 参数类型转换异常;DefaultHandlerExceptionResolver 处理框架底层的异常。response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage()); 
    package com.atguigu.admin.exception;
    
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.ResponseStatus;
    
    @ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户数量太多")
    public class UserTooManyException extends RuntimeException{
        public UserTooManyException() {
        }
    
        public UserTooManyException(String message) {
            super(message);
        }
    
    }

    3.第三种:自定义实现 HandlerExceptionResolver 处理异常

    • 自定义实现 HandlerExceptionResolver 处理异常;可以作为默认的全局异常处理规则

    • ErrorViewResolver 实现自定义处理异常;
      • response.sendError 。error请求就会转给controller
      • 你的异常没有任何人能处理。tomcat底层 response.sendError。error请求就会转给controller
      • basicErrorController 要去的页面地址是 ErrorViewResolver
    package com.atguigu.admin.exception;
    
    import org.springframework.core.Ordered;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Component
    @Order(value = Ordered.HIGHEST_PRECEDENCE) //优先级,数字越小优先级越高
    public class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {
        @Override
        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
            try {
                response.sendError(511, "我喜欢的错误");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new ModelAndView();
        }
    }

    4xx.html:

    <div class="container ">
    
            <section class="error-wrapper text-center">
                <h1><img alt="" src="images/404-error.png"></h1>
                <h2 th:text="${status}">page not found</h2>
                <h3 th:text="${message}">We Couldn’t Find This Page</h3>
                <a class="back-btn" th:href="@{/main.html}"> Back To Home</a>
            </section>
    
    </div>

     

  • 相关阅读:
    rabbitmq channel参数详解
    java中反射知识点总结
    SpringBoot的ApplicationRunner
    ServletContextInitializer添加 servlet filter listener
    如何在Job中获取 IOC applicationcontext
    QRCode.js:使用 JavaScript 生成微信二维码
    SpringBoot整合Quartz
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet解决
    javascript简介
    mysql索引
  • 原文地址:https://www.cnblogs.com/LEPENGYANG/p/15386830.html
Copyright © 2020-2023  润新知