1、新建一个springboot项目

添加一个全局异常的类

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

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/4/19 0019.
 */
@ControllerAdvice   //控制器增强
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler(value = Exception.class)//异常捕获
    public void defaultErrorHandler(HttpServletRequest req, Exception e)  {
        e.printStackTrace();
        System.out.println("GlobalDefaultExceptionHandler.defaultErrorHandler()");
    }
}

Controller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController  
@RequestMapping("/")
public class UserController {

    @RequestMapping("/testException")
    public int testException(){
        return 100/0;
    }
     
}

输入http://localhost:8080/testException

4、springboot之全局异常捕获

 

相关文章:

  • 2021-10-08
  • 2021-10-04
  • 2021-11-25
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-01
  • 2021-08-22
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案