lea*_*ser 5 java exception spring-boot
我创建了一些用于异常处理的子类。该方法抛出子类异常。我为超类和子类创建了@ExceptionHandler。但只有当没有对异常的超类进行处理(handleSuperException(SuperclassExceptionException e))时,才会处理子类异常。SubClassAException、SubClassBException、SubClassCException 扩展了 SuperclassException 异常。
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
}
Run Code Online (Sandbox Code Playgroud)
但即使抛出子类异常,它也永远不会去handleSubClassException。
无法重现!
这是最小的、可重现的示例,使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用/dmz/Spring Security 设置,因此不需要登录。在普通的 Spring Boot 设置中,这当然是不需要的。
输出 (http://localhost:8080/dmz/fail)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
输出 (http://localhost:8080/dmz/failA)
handleSuperException: web.controller.SuperclassException: failSuper()
Run Code Online (Sandbox Code Playgroud)
输出 (http://localhost:8080/dmz/failB)
handleSubClassException: web.controller.SubClassAException: failA()
Run Code Online (Sandbox Code Playgroud)