简体   繁体   中英

Spring boot @ExceptionHandler not catching subclass exception

I have made some subclass for exception handling. The method throws a subclass exception. I have made @ExceptionHandler for both super class as well as subclasses. But only if there is no handling for super class of exception (handleSuperException(SuperclassExceptionexception e)), then the subclass exceptions are handled. SubClassAException, SubClassBException, SubClassCException extends 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
}

But it never goes to handleSubClassException even if subclass exceptions are thrown.

Unable to reproduce!

Here is Minimal, Reproducible Example , tested with Spring Boot 2.2.0 (Spring 5.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);
    }
}

I'm using /dmz/ so the Spring Security setup won't require login. In a plain vanilla Spring Boot setup, that will of course not be needed.

Output ( http://localhost:8080/dmz/fail )

handleSuperException: web.controller.SuperclassException: failSuper()

Output ( http://localhost:8080/dmz/failA )

handleSubClassException: web.controller.SubClassAException: failA()

Output ( http://localhost:8080/dmz/failB )

handleSubClassException: web.controller.SubClassBException: failB()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM