简体   繁体   English

Spring @RestControllerAdvice 不处理状态码 400 异常

[英]Spring @RestControllerAdvice doesn't handle status code 400 exceptions

I have the following class for handling rest controller exceptions我有以下类用于处理休息控制器异常

@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)
    }
}

And while all status code 404 exceptions pass through notFoundException with no problems, when a status code 400 exception is thrown badRequestException is never entered.虽然所有状态码 404 异常都通过notFoundException没有问题,但当抛出状态码 400 异常时,永远不会输入badRequestException Can you help me understand what the problem here is and how to fix it?你能帮我理解这里的问题是什么以及如何解决它吗?

You are capturing these exceptions to be handled as 400 exceptions:您正在捕获这些异常以作为 400 个异常处理:

        (AccountIsNotConnectedException::class),
        (AccountCannotBeDeletedException::class),
        (InvalidVerificationSignatureException::class),
        (SubscriptionAlreadyExistsException::class),
        (ConstraintViolationException::class)

However, maybe these are not the exceptions that are being thrown.但是,也许这些不是被抛出的异常。 Check the logs for the exceptions that you throw on your code.检查日志以了解您在代码中引发的异常。 Probably you will need to add other exceptions here.可能您需要在此处添加其他例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM