我有以下类来处理rest控制器异常
@RestControllerAdvice
class RestResponseEntityExceptionHandler : ResponseEntityExceptionHandler() {
@ExceptionHandler(
value = [
(NotFoundException::class),
(AccountNotFoundException::class),
(ExecutionLogNotFoundException::class),
(SubscriptionNotFoundException::class)
]
)
protected fun notFoundException(
ex: Throwable?,
request: WebRequest?
): ResponseEntity<ErrorsDetails> {
val errorDetails = ErrorsDetails(
ex?.message!!
)
return ResponseEntity(errorDetails, HttpStatus.NOT_FOUND)
}
@ExceptionHandler(
value = [
(AccountIsNotConnectedException::class),
(AccountCannotBeDeletedException::class),
(InvalidVerificationSignatureException::class),
(SubscriptionAlreadyExistsException::class),
(ConstraintViolationException::class)
]
)
protected fun badRequestException(
ex: Throwable?,
request: WebRequest?
): ResponseEntity<ErrorsDetails> {
val errorDetails = ErrorsDetails(
ex?.message!!
)
return ResponseEntity(errorDetails, HttpStatus.BAD_REQUEST)
}
}
虽然所有状态码404异常都通过notFoundException
没有问题,但当状态码400异常被抛出时,badRequestException
从未进入。你能帮我了解这里的问题是,如何修复它吗?
您正在捕获这些异常,以便作为400个异常处理:
(AccountIsNotConnectedException::class),
(AccountCannotBeDeletedException::class),
(InvalidVerificationSignatureException::class),
(SubscriptionAlreadyExistsException::class),
(ConstraintViolationException::class)
然而,可能这些不是被抛出的异常。检查您在代码中抛出的异常日志。也许你需要在这里添加其他例外。