【发布时间】:2016-09-29 16:28:21
【问题描述】:
我正在编写一个 Spring Boot 应用程序。我现在正在实施异常处理。
我遇到了以下问题。
我的异常处理程序如下所示:
@ControllerAdvice
public class SpecialExceptionHandler {
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseObject missingServletErrorHandler(HttpServletRequest req, MissingServletRequestParameterException exception) {
//do something
return responseObject;
}}
我得到了一个看起来很通用的异常处理程序
@ControllerAdvice
public class GeneralExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseObject defaultErrorHandler(HttpServletRequest req, Exception exception) {
// do something
return responseObject;
}}
但我遇到了一个问题:我的应用程序总是运行到 GeneralExceptionHandler 而不是特殊处理程序,除非我将 GeneralExceptionHandler 类的名称更改为按字母顺序出现在特殊异常处理程序之后的名称(例如,将“GeneralExceptionHandler”更改为“zGeneralExceptionHandler” ')。
我该如何解决这个问题?
【问题讨论】:
标签: java exception-handling spring-boot