@ControllerAdvice中的Spring-boot句柄NoHandlerException [英] Spring-boot handle NoHandlerException in @ControllerAdvice

查看:69
本文介绍了@ControllerAdvice中的Spring-boot句柄NoHandlerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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

对于@Component的规范化,用于声明@ ExceptionHandler,@ InitBinder或@ModelAttribute方法要在多个@Controller类之间共享的类.

Specialization of @Component for classes that declare @ExceptionHandler, @InitBinder, or @ModelAttribute methods to be shared across multiple @Controller classes.

@Order

定义带注释组件的排序顺序.如果我们将其设置为 Ordered.HIGHEST_PRECEDENCE ,则该常量对于获得最高优先级值很有用.

Defines the sort order for an annotated component. If we put as Ordered.HIGHEST_PRECEDENCE which is useful constant for the highest precedence value.

代码应显示如下:

@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class ExceptionsHandler extends ResponseEntityExceptionHandler {
    .....
}

参考:

@ControllerAdvice注释文档

@订单注释文档

这篇关于@ControllerAdvice中的Spring-boot句柄NoHandlerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆