springboot全局异常处理(1)
新建一个类 在类上加一个注解即可 @ControllerAdvice
/** * 全局错误处理 * @author sys * */ @ControllerAdvice @ResponseBody public class GlobalExceptionHandler { private final Logger log = Logger.getLogger(this.getClass()); @ExceptionHandler(value=Exception.class) //拦截异常的种类 public Object MethodArgumentNotValidHandler(HttpServletRequest request, Exception ex) throws Exception { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); log.error(sw.toString()); ResData res = new ResData(0,"请求错误"); return JSON.toJSONString(res); } }