有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring boot@ExceptionHandler未捕获子类异常

我已经为异常处理创建了一些子类。该方法引发子类异常。我为超类和子类制作了@ExceptionHandler。但只有在没有处理异常的超类(handleSuperException(SuperclasseExceptionException e))时,子类异常才会被处理。SubClassException,SubClassBeException,SubClassException扩展了SuperclassExceptionexception

 public class Controller @PostMapping("/path/") {
       public ResponseEntity<String> method() throws   SuperclassException{
 }
    @ExceptionHandler(SuperclassException.class)   
public ResponseEntity handleSuperException(SuperclassExceptionexception e) {
       //hadle superclass related
    }

 @ExceptionHandler({SubClassAException.class, SubClassBException.class, SubClassCException.class}) 
public ResponseEntity handleSubClassException(SuperclassExceptionexception e) {
//handle more specific
}

但即使抛出子类异常,它也永远不会进入handleSubClassException


共 (1) 个答案

  1. # 1 楼答案

    无法复制

    这里是Minimal, Reproducible Example,用SpringBoot2.2.0(Spring5.2.0)测试

    package web.controller;
    
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class FailController {
    
        @GetMapping("/dmz/fail")
        public String failSuper() {
            throw new SuperclassException("failSuper()");
        }
    
        @GetMapping("/dmz/failA")
        public String failA() {
            throw new SubClassAException("failA()");
        }
    
        @GetMapping("/dmz/failB")
        public String failB() {
            throw new SubClassBException("failB()");
        }
    
        @ExceptionHandler(SuperclassException.class)
        public ResponseEntity<?> handleSuperException(SuperclassException e) {
            return ResponseEntity.badRequest().body("handleSuperException: " + e);
        }
    
        @ExceptionHandler({SubClassAException.class, SubClassBException.class}) 
        public ResponseEntity<?> handleSubClassException(SuperclassException e) {
            return ResponseEntity.badRequest().body("handleSubClassException: " + e);
        }
    
    }
    
    class SuperclassException extends RuntimeException {
        public SuperclassException(String message) {
            super(message);
        }
    }
    
    class SubClassAException extends SuperclassException {
        public SubClassAException(String message) {
            super(message);
        }
    }
    
    class SubClassBException extends SuperclassException {
        public SubClassBException(String message) {
            super(message);
        }
    }
    

    我正在使用/dmz/,因此Spring安全设置不需要登录。在普通的Spring引导设置中,当然不需要这样做

    输出(http://localhost:8080/dmz/fail

    handleSuperException: web.controller.SuperclassException: failSuper()
    

    输出(http://localhost:8080/dmz/failA

    handleSubClassException: web.controller.SubClassAException: failA()
    

    输出(http://localhost:8080/dmz/failB

    handleSubClassException: web.controller.SubClassBException: failB()