HOWTO handle 404 exceptions globally using Spring MVC configured using Java based Annotations

后端 未结 5 1873
眼角桃花
眼角桃花 2021-01-02 03:05

I am building a Spring 4 MVC app. And it is completely configured using Java Annotations. There is no web.xml. The app is configured by using instance of

相关标签:
5条回答
  • 2021-01-02 03:42

    Instead overriding registerDispatcherServlet one can override the createDispatcherServlet method as follows.

    @Override
        protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
            DispatcherServlet ds = new DispatcherServlet(servletAppContext);
            ds.setThrowExceptionIfNoHandlerFound(true);
            return ds;
        }
    
    0 讨论(0)
  • 2021-01-02 03:43

    Enable DispatcherServlet throw a NoHandlerFoundException through web.xml configuartion.

    <init-param>
        <param-name>throwExceptionIfNoHandlerFound</param-name>
        <param-value>true</param-value>
    </init-param>
    
    0 讨论(0)
  • 2021-01-02 03:53

    I resolved the problem with the following entry in my application.yml

     server.error.whitelabel.enabled: false
     spring.mvc.throw-exception-if-no-handler-found: true
    

    and the following ControllerExceptionHandler:

    @ControllerAdvice
    public class ControllerExceptionHandler {
    
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String processMethodNotSupportedException(Exception exception) {
        exception.printStackTrace();
        return "error";
    }
    
    }
    

    and last but not least i added a template "/html/error.html"

    0 讨论(0)
  • 2021-01-02 03:57

    By default, the DispatcherServlet does not throw a NoHandlerFoundException. You need to enable that.

    The AbstractAnnotationConfigDispatcherServletInitializer should let you override how the DispatcherServlet is created. Do that and call

    DispatcherServlet dispatcherServlet = ...; // might get it from super implementation
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    
    0 讨论(0)
  • 2021-01-02 03:57

    I can't comment on the above post by @Ysak (reputation<50), however I can confirm that this method does work with the setup described by the OP.

    I will add that from another guide I also had the DefaultServletHandling configured in my WebConfig to fix a separate issue, as below:

    @Configuration
    @EnableWebMvc
    @ComponentScan("com.acme.tat.controllers")
    public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
    ...
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
    }
    
    }
    

    With this set to enable, there is no Exception thrown. Once I removed this line and set up manual resourceHandlers everything worked as expected.

    It took me around 2.5 hours to set up 404 redirecting because of this simple one line method.

    0 讨论(0)
提交回复
热议问题