@ exceptionHandlerの+ @ ControllerAdviceを使用して、[アーキテクチャ]春ブーツグローバル例外ハンドラ

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/sinat_27933301/article/details/101949881
コメント 定義
exceptionHandlerの 注釈の方法、作用コントローラレベルは、exceptionHandlerの注釈はのcontroler例外ハンドラを定義します
ControllerAdvice クラスのノートは、全体の春のプロジェクトに作用し、ControllerAdvice注釈は、グローバル例外ハンドラを定義します

  、exceptionHandlerのControllerAdviceより高い優先順位、すなわち治療のexceptionHandlerの標識方法を優先することに注意してください。

/**
 * 全局异常处理
 */
@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