异常处理器
在异常处理器类上使用@ControllerAdvice注解,异常处理器方法上使用@ExceptionHandler注解指定要处理的异常,之后在控制器中抛出异常时,异常处理器中匹配的方法就会执行
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class MyException {
@ExceptionHandler(value = {Exception.class}) //数组形式,可指定多个异常
@ResponseBody
public String error(Exception e) { //与控制器中的方法用法一致
return "出现异常" + e.getMessage();
}
}

Comments NOTHING