问题描述
我想在Springboot应用程序中处理NoHandlerException并返回自定义错误消息.我在我的application.properties中添加了以下内容,并尝试覆盖错误消息.
I want to handle NoHandlerException in Springboot app and return a custom error message. I added following to my application.properties and tried to override the error message.
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
错误没有达到@ControllerAdvice ...它在defaulthandlerexceptionresolver中处理.有什么想法吗?
Error doesn't hit the @ControllerAdvice... It is handled in defaulthandlerexceptionresolver . Any ideas?
推荐答案
在异常处理程序头上放入 @Order(Ordered.HIGHEST_PRECEDENCE)
和 @ControllerAdvice
:
Putting @Order(Ordered.HIGHEST_PRECEDENCE)
and @ControllerAdvice
on the exceptions handler head, it means:
@ControllerAdvice
@Order
代码应显示如下:
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class ExceptionsHandler extends ResponseEntityExceptionHandler {
.....
}
参考:
这篇关于@ControllerAdvice中的Spring-boot句柄NoHandlerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!