[건축] 봄 부팅 글로벌 예외 처리기 사용하여 @ ExceptionHandler + @ ControllerAdvice

면책 조항 :이 문서는 블로거 원본입니다, 추적 에 의해-SA의 CC 4.0 저작권 계약, 복제, 원본 소스 링크이 문을 첨부 해주세요.
이 링크 : https://blog.csdn.net/sinat_27933301/article/details/101949881
논평 정의
ExceptionHandler 주석에있어서, 작용 컨트롤러 레벨은 ExceptionHandler 주석은 CONTROLER 예외 핸들러를 정의
ControllerAdvice 클래스 노트는 전체 봄 프로젝트에 작용, ControllerAdvice 주석 글로벌 예외 핸들러를 정의

  그, ExceptionHandler 즉 치료의 ExceptionHandler 라벨링 방법에 우선 순위를 부여 ControllerAdvice보다 더 높은 우선 순위, 있습니다.

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseStatus(code = HttpStatus.NOT_FOUND)
    public String e404() {
        return "error/404.html";
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
    public String e500() {
        return "error/500.html";
    }
}
@Controller
public class UserController {

    /**
	 * 局部异常处理
	 */
    @ExceptionHandler(BusinessException.class)
    public String exPage(Exception ex, Model model) {
        model.addAttribute("ex", ex);

        return  "/error/business.html";
    }
}

봄 부팅 기본 리소스 경로, 뷰 클래스 ResourceProperties 스프링 부팅 자동 구성 패키지로 제공된다.

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
     "classpath:/META-INF/resources/",
     "classpath:/resources/", 
     "classpath:/static/", 
     "classpath:/public/"};

추천

출처blog.csdn.net/sinat_27933301/article/details/101949881