一、代码如下
package com.zxguan; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import javax.servlet.http.HttpServletRequest; /** * @author zxguan * @description * @create 2018-01-29 10:38 */ @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(value = NullPointerException.class) public void nullPointErrorHandler(HttpServletRequest req, Exception e) { e.printStackTrace(); System.out.println("------------------------ NullPointerException --------------------------"); } @ExceptionHandler(value = Exception.class) public void defaultErrorHandler(HttpServletRequest req, Exception e) { e.printStackTrace(); System.out.println("------------------------ Exception --------------------------"); } }
二、要点
1、@ControllerAdvice
1)basePackages:全局异常捕获包范围
2)basePackageClasses:扫描指定类所在包范围【通常使用一个标记类或接口,通过注解中basePackageClasses定义解析包范围】