1

What happens when all of the request mappings in the controller do not match the incoming request? I was using the exceptionhandler catching Exception.class but that did not seem to catch it? Can anyone explain?

Alan Chan
  • 11
  • 2
  • If you have two mappings one for root `/` and another for say `/api` it will do closest match. Only api will get handled by /api but / and others will match the root I believe. Then you can handle whatever you like it /. Have you tried ? – bhantol Jan 03 '17 at 01:34
  • Yes, currently it is doing a GET request but because there are no mappings for that, (all are POST request mappings) I was wondering how I can catch the error/exception (whatever leads it to a HTTP 400 page) and not do a request mapping for a GET – Alan Chan Jan 03 '17 at 01:43
  • If i correctly understand you. If comes a request that is not mapped result will be a page with 404 status code. Maybe you don't implements correctly your handler. If your project contains web.xml you may configure this using `` tag ; In case it is based on annotation configuration view related post : http://stackoverflow.com/questions/13356549/handle-error-404-with-spring-controller and if you are using spring boot define bean `EmbeddedServletContainerCustomizer` and there configure Error page, that you will handle in controller. – Vladlen Gladis Jan 03 '17 at 07:16

1 Answers1

1

According to documentation @ExceptionHandler deal with unexpected exceptions that occur during controller execution. In the case when incoming request do not match any request mapping, controller method even will not be executed.

Spring-MVC based on Servlet technology. All incoming requests will be processed by DispatcherServlet (that registered in web.xml). DispatcherServlet looks for handlers which can process current request. If no appropriate handler will be found (i.e. no appropriate mapped controller's method will be found), DispatcherServlet initiates 404 error. When an error occurs, the web container(Tomcat or any other servlet container) generates a default page containing exception message. But you can also specify that the container should return a specific error page for a given exception. For process this error manually you should add into web.xml new error-page element, that will allow you map error code to url:

...

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<error-page>
    <error-code>404</error-code>
    <location>/page-not-found</location>
</error-page>

...

Because location will be processed by Spring-MVC as usual request you can create controller to handle this request:

@Controller
public class ExceptionController{

    @RequestMapping("/page-not-found")
    public ModelAndView pageNotFound(){
        return new ModelAndView("page-not-found");
    }

}

Now just create view with name page-not-found.jsp and show exception info as you wish.

Ken Bekov
  • 13,696
  • 3
  • 36
  • 44