Spring ControllerAdvice中未处理404异常

Spring ControllerAdvice中未处理404异常,第1张

Spring ControllerAdvice中未处理404异常

要使其工作,您需要

throwExceptionIfNoHandlerFound
在DispecherServlet 上设置属性。您可以执行以下 *** 作:

spring.mvc.throwExceptionIfNoHandlerFound=true

application.properties
文件中,否则请求将始终转发默认servlet,并且将引发NoHandlerFoundException。

问题是,即使使用此配置,它也不起作用。从文档中:

请注意,如果使用org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,则请求将始终转发到默认servlet,并且在这种情况下永远不会引发NoHandlerFoundException。

由于默认情况下Spring
Boot使用,因此

org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
您必须使用自己的方法覆盖它
WebMvcConfigurer

import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@EnableWebMvc@Configurationpublic class WebConfig implements WebMvcConfigurer {    @Override    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {        // Do nothing instead of configurer.enable();    }}

当然,上述情况在您的情况下可能会更复杂。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5011979.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存