9
10

More than 5 years have passed since last update.

SpringBootでエラーハンドリング

Last updated at Posted at 2015-08-23

Spring Bootでエラーハンドリングをする方法はいくつかあります。
この中の2つをご紹介。

1.@ControllerAdviceでの実装

ExceptionControllerAdvice.java
     @ControllerAdvice
     public class ExceptionControllerAdvice {

     @InitBinder
     public void initBinder(WebDataBinder binder) {
        System.out.println("controller advice: init binder");
    }


    @ExceptionHandler(Exception.class)
    public String exception(Exception e) {
        System.out.println("controller advice: exception Handler");
        System.out.println(e.getMessage());
        return "error";
    }

    @ModelAttribute
    public void modelAttribute(){
        System.out.println("controller advice:model Attribute");
    }
    }

2.コントローラー内に直接記述

XXController.java
@RequestMapping("/xx")
public class XXController {
    @ExceptionHandler(Exception.class)
    public String handling(Exception e) {
        return "/err";
    }
}
9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10