Spring Boot入門見本例-130-excetionグローバル異常処理を統一する


Spring Boot入門見本例-130-excetionグローバル異常処理を統一する
プログラム実行中には様々なエラーが発生する可能性があります.各場所にtry catchを加えるのは面倒くさいです.本デモでは,異常を一箇所で一括処理する方法を示し,開発コードを減らす.
前言
本Spring Boot入門見本準備作業参考:
  • Spring Boot入門例-001-JavaとMavenの設置配置
  • Spring Boot入門例-003-indeaインストール構成とプラグイン
  • Spring Boot入門例-005-どうやって
  • を実行しますか?
    Spring Boot提供@RertController Adviceと@Exception Handlerの二つの注釈は大域統一異常処理を完成しました.
    pox.xml
    必要な依存性は以下の通りです.具体的にはこの項目のpox.xmlを参照してください.
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                com.alibaba
                fastjson
                1.2.54
            
    
            
                org.projectlombok
                lombok
                true
            
    
    設定ファイル
    resource/appection.yml設定内容ehcache.com fig指定ehcacheの
     
    
    コード解析
    BootanException.javaは以下のようにカスタマイズされています.
    /**
     *      
     *
     * @author Funsonli
     * @date 2019/11/19
     */
    @Data
    public class BootanException extends RuntimeException {
    
        private String message;
    
        public BootanException(String message) {
            super(message);
            this.message = message;
        }
    
    }
    
    
    
    Reset Exception Handler.javaは以下のようにReset方式のデータをまとめて返します.
    /**
     *      
     *
     * @author Funsonli
     * @date 2019/11/19
     */
    @Slf4j
    @RestControllerAdvice
    public class RestExceptionHandler {
    
        @ExceptionHandler(BootanException.class)
        @ResponseStatus(HttpStatus.OK)
        public BaseResult handleBootanException(BootanException e) {
    
            if (e != null) {
                log.info(e.toString(), e);
                return BaseResult.error(e.getMessage());
            }
            return BaseResult.error();
        }
    
        @ExceptionHandler(Exception.class)
        @ResponseStatus(HttpStatus.OK)
        public BaseResult handleException(BootanException e) {
    
            if (e != null) {
                log.info(e.toString(), e);
                return BaseResult.error(e.getMessage());
            }
            return BaseResult.error();
        }
    
    }
    
    
    StudentController.javaは以下のようにそれぞれカスタム異常とシステム異常を使用します.
    @Slf4j
    @RestController
    @RequestMapping("/student")
    public class StudentController {
    
        @GetMapping({"", "/", "index"})
        public String index() {
            throw new BootanException("index error");
        }
    
        @GetMapping("/view/{id}")
        public String view(@PathVariable Integer id) throws Exception {
    
            int i = 3 / id;
            return id + " id divide by 3 ";
        }
    }
    
    実行
    クリックして実行
          http://localhost:8080/student/
    {"code":500,"data":null,"message":"index error","status":500,"timestamp":1574134955395}
    
    
          http://localhost:8080/student/view/1
    1 id divide by 3
    
    Postman   http://localhost:8085/student/view/0
    {
        "timestamp": "2019-11-19T03:39:22.284+0000",
        "status": 500,
        "error": "Internal Server Error",
        "message": "/ by zero",
        "path": "/student/view/0"
    }
    
    参照
  • Spring Boot入門サンプルソースコードアドレスhttps://github.com/funsonli/spring-boot-demo
  • Bootanソースコードアドレスhttps://github.com/funsonli/bootan
  • 付属する
    本Spring Boot入門の見本とサンプルコードが好きなら、素敵なスターを注文してください.