springboot错误统一处理

1,对于404,500这类错误,可以直接新建public/error目录 ,在error目录 中新建404.html, 500.html或5xx.html,springboot会自动跳转到这些静态页面。

2,如果想记录错误信息,可以通过专门的类来处理。通过@ControllerAdvice注解


@ControllerAdvice
public class ErrorControllerAdvice {

@ExceptionHandler(Exception.class)
public void handler(Exception ex, HandlerMethod handlerMethod){
Log log = LogFactory.getLog(ErrorControllerAdvice.class);
log.info(ex.getMessage());
System.out.println(handlerMethod.getMethod().getName());
System.out.println(handlerMethod.getClass().getName());
}
}

 

posted @ 2020-01-29 17:44  王东波  阅读(81)  评论(0编辑  收藏  举报