404异常春ControllerAdvice没有处理

莱昂纳多Chirivì:

我有,我想处理使用的所有未映射的网址,一个简单的Spring MVC应用程序@ControllerAdvice这里是控制器:

@ControllerAdvice
public class ExceptionHandlerController {
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public String handle404() {
        return "exceptions/404page";
    }
}

尽管如此,每次都获得白色标签错误页面。

我尝试使用RuntimeException.classHttpStatus.BAD_REQUEST并且延伸至与类NoHandlerFoundException,但没有用。

有什么建议么?

尤金Covaci:

为了使它的工作,你需要设置throwExceptionIfNoHandlerFound上DispecherServlet财产。你可以做到这一点:

spring.mvc.throwExceptionIfNoHandlerFound=true

application.properties文件中,否则请求都将被转发到默认的servlet和NoHandlerFoundException将永远被抛出。

问题是,即使有这样的配置,这是行不通的。从文档:

请注意,如果使用org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,然后请求都将被转发到默认的servlet和NoHandlerFoundException绝不会在这种情况下被抛出。

因为春天开机默认使用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
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // Do nothing instead of configurer.enable();
    }
} 

当然,上面的类可能更复杂,你的情况。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

@ControllerAdvice不处理异常

如何处理@ControllerAdvice中所有未处理的异常?

Spring ControllerAdvice无法处理静态方法的异常

如何涵盖专门处理异常的ControllerAdvice

如何在@ControllerAdvice中测试异常处理

@ControllerAdvice异常处理程序方法未调用

与@ExceptionHandler或HandlerExceptionResolver相比,@ ControllerAdvice在处理异常方面有什么优势?

没有Wait()的任务的异常处理

Spring ControllerAdvice和身份验证/授权异常处理

Spring中的自定义@ControllerAdvice,用于异常处理

春季启动:使用ControllerAdvice for REST处理自定义异常

Spring Boot @ControllerAdvice异常处理程序未触发

使用 @ControllerAdvice 注释类处理异常时丢失堆栈跟踪

如何使用 Spring REST Docs 记录 @ControllerAdvice 处理的异常

@ControllerAdvice 中的异常处理程序方法不会被调用

里面HttpMessageConversionException UnrecognizedPropertyException没有被解开的ControllerAdvice

Spring ControllerAdvice没有捕获SizeLimitExceededException?

@ControllerAdvice没有映射到错误

如果没有结果,Quarkus Mutiny 多异常处理

在没有视图的 ViewModel 中处理异常(在 ReactiveUI 中)

sql中没有返回行时如何处理异常

Twisted Deferred不显示未处理的异常而没有errback

Spring MVC如何使用Java配置和没有Web.xml来处理404页面未找到异常

Python:如何在没有“当前正在处理”异常的情况下打印异常对象的stacktrace?

Java异常-在没有try catch的情况下处理异常

在WebFilter类中引发异常,并在ControllerAdvice类中处理该异常

@ControllerAdvice覆盖异常@ResponseStatus

没有捕获异常

异常没有冒泡