在我的机器上找不到ExceptionHandler,但是在开发服务器上

时间:2018-11-02 15:59:20

标签: spring exception-handling handlerexceptionresolver

我构建了一个REST-API,其中所有问题的解决方法是抛出异常,然后在包含多个@ExceptionHandler方法的@ControllerAdvice带注释的类中进行处理。

出于某种原因,当我运行集成测试(使用货运服务器)时,或者在本地tomcat 8服务器中运行战争时,没有捕获到异常,但是当我在服务器上运行战争时,则捕获了异常开发服务器。据我所能确定的,dev tomcat的配置没有不同。

我的@ControllerAdvice如下所示:

@ControllerAdvice
public class CustomResponseEntityExceptionHandler {

@ExceptionHandler(PasswordMismatchException.class)
public final ResponseEntity<ErrorDetails> handlePasswordChangeMismatch(
        PasswordMismatchException ex,
        WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails(
            new Date(),
            ex.getMessage(),
            request.getDescription(false),
            MessageKeys.mismatchedPassword);
    return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(DuplicateUserException.class)
public final ResponseEntity<ErrorDetails> handleDuplicateUser(
        DuplicateUserException ex,
        WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails(
            new Date(),
            ex.getMessage(),
            request.getDescription(false),
            MessageKeys.duplicateUsername);
    return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(LastAdminDeletionException.class)
public final ResponseEntity<ErrorDetails> handleDuplicateUser(
        LastAdminDeletionException ex,
        WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails(
            new Date(),
            ex.getMessage(),
            request.getDescription(false),
            MessageKeys.lastAdminDeletion);
    return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(WeakPasswordException.class)
public final ResponseEntity<ErrorDetails> handleDuplicateUser(
        WeakPasswordException ex,
        WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails(
            new Date(),
            ex.getMessage(),
            request.getDescription(false),
            MessageKeys.weakPassword);
    return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}
}

你们听起来熟悉吗?

0 个答案:

没有答案