title: Spring Boot - Handling NoHandlerFoundException
body: <p>Read the <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet-sequence" rel="noreferrer">Spring Reference Guide on how to handle NoHandlerFoundException</a> and found that Spring sets, by default, <code>throwExceptionIfNoHandlerFound</code> to <code>false</code>.</p>
  
  <p>Knowing that, I thought it was a good idea to set this parameter to <code>true</code>.</p>
  
  <p>I'm using Spring Boot.</p>
  
  <p>Did so:</p>
  
  <p><strong>MyConfig.java</strong></p>
  
  <pre><code>import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ConfigurableApplicationContext;
  import org.springframework.web.servlet.DispatcherServlet;
  
  @SpringBootApplication
  public class MyConfig {
  
      public static void main(String[] args) {
          ConfigurableApplicationContext context = SpringApplication.run(MyConfig.class, args);
          context.getBean(DispatcherServlet.class).setThrowExceptionIfNoHandlerFound(true);
  
          // ...
      }
  }
  </code></pre>
  
  <p>Alright, now <code>throwExceptionIfNoHandlerFound</code> is equals <code>true</code>. However, that didn't worked as expected. The <code>DispatcherServlet</code> continued not throwing the <code>NoHandlerFoundException</code>. This way, I was unable to handle it.</p>
  
  <p><strong>CustomExceptionHandler.java</strong> <em>(this didn't worked)</em></p>
  
  <pre><code>import org.springframework.http.HttpHeaders;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.context.request.WebRequest;
  import org.springframework.web.servlet.NoHandlerFoundException;
  import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
  
  @ControllerAdvice
  public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
  
      @Override
      protected ResponseEntity &lt;Object&gt; handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
          // do my own handle ...
  
          // then, return ...
      }
  
  }
  </code></pre>
  
  <p>After a bit of search, found that adding <code>@EnableWebMvc</code> should work. Then, I did add this annotation to my <code>CustomExceptionHandler</code></p>
  
  <p><strong>CustomExceptionHandler.java</strong> <em>(this did worked)</em></p>
  
  <pre><code>import org.springframework.http.HttpHeaders;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.context.request.WebRequest;
  import org.springframework.web.servlet.NoHandlerFoundException;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
  
  @EnableWebMvc
  @ControllerAdvice
  public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
  
      @Override
      protected ResponseEntity &lt;Object&gt; handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
          // do my own handle ...
  
          // then, return ...
      }
  
  }
  </code></pre>
  
  <p>This way, the handle works. However, I'm using Spring Boot. The Spring Boot docs suggests that if I insert <code>@EnableWebMvc</code>, I would lose some Spring Boot MVC Auto-configuration features, since I would take complete controll of Spring MVC. (<a href="https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration" rel="noreferrer">See here</a>).</p>
  
  <blockquote>
    <p>"If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc."</p>
  </blockquote>
  
  <p>Would I lose Spring MVC Auto-configuration? I mean, the <code>@EnableWebMvc</code>, in my case, it's not in a <code>@Configuration</code> class.</p>
  
  <p>My question is based on the fact that I'm not sure how this code above works, and I want to know if there's another way of doing this without losing the Spring MVC Auto-configuration. Could someone explain?</p>