在class注解上@ControllerAdvice,

在方法上注解上@ExceptionHandler(value = Exception.class),具体代码如下:

package me.shijunjie.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public void handleGlobalException(HttpServletRequest req, Exception e) {
        //打印异常信息:
        //e.printStackTrace();
        System.out.println("GlobalExceptionHandler.handleGlobalException()");
    }

}

 

@RequestMapping("/zeroException")
    public int zeroException(){
       return 111/0;
    }

 

相关文章:

  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-08-22
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-22
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
  • 2021-05-21
相关资源
相似解决方案