全局异常的处理:
1.新建一个类,例如GlobalExceptionHandler
2.类上添加@ControllerAdvice
3.添加方法
4.方法上添加@ExceptionHandler
5.若返回为String或Json数据,添加@ResponseBody
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public String sendException(HttpServletRequest req,Exception e){
return "error,waiting...";
}
}