ControllerExceptionHandler 无法在 spring 引导应用程序上运行
ControllerExceptionHandler not working on a spring boot app
我有一个带有 ControllerAdvise 的 ControllerExceptionHandler,当在应用程序中抛出 generalException 或 customException 时,我除了在这个 ControllerExceptionHandler 中捕获它。但它不会发生。看起来很简单,看了很多网站,但是就是不触发。
不知道哪里出了问题。
是ControllerExceptionHandler.class
@ControllerAdvice
public class ControllerExceptionHandler extends ResponseEntityExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(ControllerExceptionHandler.class);
@ExceptionHandler(Exception.class)
public final ResponseEntity<?> handleGeneralErrors(Exception e, UserPrincipal userPrincipal) {
if (e instanceof ClientAbortException) {
LOGGER.error("REST client abort: {}", e.getMessage());
} else {
LOGGER.error("REST controller error.", e);
}
//somelogic
return new ResponseEntity<>(responseObject, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(MyCustomException.class)
public final ResponseEntity<?> handleCustomErrors(MyCustomException e, UserPrincipal userPrincipal) {
LOGGER.error("MyCustomException error.", e);
return new ResponseEntity<>(
responseObject,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Controller.class
@RestController
@RequestMapping(path = "/rest/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class Controller {
private static final Logger LOGGER = LoggerFactory.getLogger(Controller .class);
@Autowired
private SomeLogicClass someLogicClass;
@GetMapping("/check")
@ResponseBody
public ResponseEntity<List<City>> list(UserPrincipal userPrincipal) throws Exception {
//Some logic
return ResponseEntity.ok(someLogicClass.handleRequest());
}
SomeLogicClass.class
@Service
public class SomeLogicClass{
public void handleRequest() throws Exception {
//some logic
} catch (Exception ex) {
throw new MyCustomException();
}
}
}
因此,当请求到达 SomeLogicClass 时,会抛出一些异常(例如 NPE),然后我想抛出 catch myCustomException。我希望它会转到 ControllerAdvise,但什么也没有,我看到错误 500。
这里缺少什么东西还是什么? Controller 和 ControllerAdvise 位于某个包中。
我也试过添加包
@ControllerAdvice("my.package.controller")
找到导致它跳过 ControllerAdvise 的原因。问题是 UserPrinciple 对象,它实际上是我的自定义 class。我在输入参数中额外添加的任何自定义 class 都是问题。我不知道这样做的原因,但在没有 UserPrinciple.class.
的情况下实施
我有一个带有 ControllerAdvise 的 ControllerExceptionHandler,当在应用程序中抛出 generalException 或 customException 时,我除了在这个 ControllerExceptionHandler 中捕获它。但它不会发生。看起来很简单,看了很多网站,但是就是不触发。 不知道哪里出了问题。
是ControllerExceptionHandler.class
@ControllerAdvice
public class ControllerExceptionHandler extends ResponseEntityExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(ControllerExceptionHandler.class);
@ExceptionHandler(Exception.class)
public final ResponseEntity<?> handleGeneralErrors(Exception e, UserPrincipal userPrincipal) {
if (e instanceof ClientAbortException) {
LOGGER.error("REST client abort: {}", e.getMessage());
} else {
LOGGER.error("REST controller error.", e);
}
//somelogic
return new ResponseEntity<>(responseObject, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(MyCustomException.class)
public final ResponseEntity<?> handleCustomErrors(MyCustomException e, UserPrincipal userPrincipal) {
LOGGER.error("MyCustomException error.", e);
return new ResponseEntity<>(
responseObject,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Controller.class
@RestController
@RequestMapping(path = "/rest/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class Controller {
private static final Logger LOGGER = LoggerFactory.getLogger(Controller .class);
@Autowired
private SomeLogicClass someLogicClass;
@GetMapping("/check")
@ResponseBody
public ResponseEntity<List<City>> list(UserPrincipal userPrincipal) throws Exception {
//Some logic
return ResponseEntity.ok(someLogicClass.handleRequest());
}
SomeLogicClass.class
@Service
public class SomeLogicClass{
public void handleRequest() throws Exception {
//some logic
} catch (Exception ex) {
throw new MyCustomException();
}
}
}
因此,当请求到达 SomeLogicClass 时,会抛出一些异常(例如 NPE),然后我想抛出 catch myCustomException。我希望它会转到 ControllerAdvise,但什么也没有,我看到错误 500。 这里缺少什么东西还是什么? Controller 和 ControllerAdvise 位于某个包中。
我也试过添加包
@ControllerAdvice("my.package.controller")
找到导致它跳过 ControllerAdvise 的原因。问题是 UserPrinciple 对象,它实际上是我的自定义 class。我在输入参数中额外添加的任何自定义 class 都是问题。我不知道这样做的原因,但在没有 UserPrinciple.class.
的情况下实施