首页 > 解决方案 > Spring @ExceptionHandler 不适用于 IllegalArgumentException

问题描述

我在 Kotlin Spring 启动项目中获得了一个 API,并且正在自定义我的错误响应。我有一个用于处理异常以显示自定义错误响应的类。我添加了 2 个处理程序。他们完美地工作:


@ControllerAdvice
class ControllerExceptionHandlers {

  @ExceptionHandler(ConstraintViolationException::class)
  fun constraintViolationException(e: ConstraintViolationException): ResponseEntity<Any> {
    return ResponseEntity
      .status(MyManError.BAD_PARAM_VALIDATION.status)
      .body(
        Error(
          FaultDefinition(
            MyManError.BAD_PARAM_VALIDATION.code,
            MyManError.BAD_PARAM_VALIDATION.name,
            "Parameter type mismatch field: '${e.message}'",
            "Source Name: ${Security.getUserName()}",
            null
          )
        )
      )
  }
  @ExceptionHandler(MissingServletRequestPartException::class)
  fun missingServletRequestPartException(e: MissingServletRequestPartException): ResponseEntity<Any> {
    return ResponseEntity
      .status(MyManError.BAD_PARAM_VALIDATION.status)
      .body(
        Error(
          FaultDefinition(
            MyManError.BAD_PARAM_VALIDATION.code,
            "BAD_PARAM_${e.requestPartName.toUpperCase()}",
            "Parameter validation failed for field $e.requestPartName",
            "Source Name: ${Security.getUserName()}",
            null
          )
        )
      )
  }
}

我想再添加 1 个错误处理程序IllegalArgumentException

  @ExceptionHandler(IllegalArgumentException::class)
  fun illegalArgumentException(e: IllegalArgumentException): ResponseEntity<Any> {
    return ResponseEntity
      .status(MyManError.BAD_PARAM_VALIDATION.status)
      .body(
        Error(
          FaultDefinition(
            MyManError.BAD_PARAM_VALIDATION.code,
            MyManError.BAD_PARAM_VALIDATION.name,
            "'${e.message}'",
            "Source Name: ${Security.getUserName()}",
            null
          )
        )
      )
  }

我添加server.max-http-header-size并分配了 10KB。所以我尝试发送 11KB 的请求。但是我在日志中得到了这样的错误:

17:09:42.513 [http-nio-8080-exec-3] INFO  o.a.coyote.http11.Http11Processor - Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Request header is too large
    at org.apache.coyote.http11.Http11InputBuffer.parseHeaders(Http11InputBuffer.java:603)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:284)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)

我完全确定我正在导入,import java.lang.IllegalArgumentException但它仍然显示这样的日志。

为什么 @ExceptionHandler 不能使用IllegalArgumentException?其他处理程序运行良好,但事实并非如此。你能帮助我吗 ?

编辑:一件奇怪的事情是;如果我为 IllegalArgumentException 添加处理程序,但如果我做不同的事情,我可以看到我的自定义响应(这正是我想要的):

{
    "fault": {
        "code": 420018,
        "message": "BAD_PARAM_VALIDATION",
        "description": "'No topic found for name :'my-events.fifo''",
        "source": "Source Name: Wicaledon"
    }
}

我完全确定新处理程序处理了它,因为如果我删除它,并且如果我检查日志,我会看到与自定义响应中相同的错误:

java.lang.IllegalArgumentException: No topic found for name :'my-events.fifo'
    at io.awspring.cloud.messaging.support.destination.DynamicTopicDestinationResolver.getTopicResourceName(DynamicTopicDestinationResolver.java:94)
    at io.awspring.cloud.messaging.support.destination.DynamicTopicDestinationResolver.resolveDestination(DynamicTopicDestinationResolver.java:72)
    at io.awspring.cloud.messaging.support.destination.DynamicTopicDestinationResolver.resolveDestination(DynamicTopicDestinationResolver.java:36)
    at org.springframework.messaging.core.CachingDestinationResolverProxy.resolveDestination(CachingDestinationResolverProxy.java:92)
    at io.awspring.cloud.messaging.core.support.AbstractMessageChannelMessagingSendingTemplate.resolveMessageChannelByLogicalName(AbstractMessageChannelMessagingSendingTemplate.java:94)
    at io.awspring.cloud.messaging.core.support.AbstractMessageChannelMessagingSendingTemplate.convertAndSend(AbstractMessageChannelMessagingSendingTemplate.java:75)

标签: javaspringkotlinexceptionillegalargumentexception

解决方案


推荐阅读