Handle error 404 in web application - not REST application

后端 未结 3 595
不知归路
不知归路 2021-01-05 23:10

I\'m trying to handle 404 error using an @ControllerAdvice in a Spring MVC application totally configured using Javaconfig.

Spring MVC version is 4.1.5<

3条回答
  •  醉话见心
    2021-01-05 23:57

    Conclusion seems to be that setting throwExceptionIfNoHandlerFound to true does not throw an exception when no handler is found.

    The solution is quite simple. From the javadoc @ DispatcherServlet.setThrowExceptionIfNoHandlerFound. It states here that a NoHandlerFoundException will never be thrown if DefaultServletHttpRequestHandler is used.

    Solution hence is to remove the line

       configurer.enable();
    

    from your MvcConfiguration. The exception should fire now and your GlobalExceptionHandler should do the rest!

提交回复
热议问题