java - 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)
解决方案
推荐阅读
- python - 将 HTML 类添加到带有扩展名的 Sphinx 指令
- php - 未创建 Symfony 4.1 缓存
- concurrency - 不是从 PMCMD 命令开始的并发工作流
- node.js - 仅覆盖 Node 中类的构造函数
- bash - 如何强制为我的链接创建目录
- mysql - 以最大值 (100%) 进行计算
- concurrency - CompletableFuture.allOf,记录并重新抛出异常
- backend - 包含用户特定数据的 Dialogflow
- python - 计算熊猫数据框中最近邻居的平均距离
- apache-spark - 最新 Parquet 版本的 Spark