title: How to handle 404 page not found exception in Spring MVC with java configuration and no Web.xml
body: <p>I want to handle 404 page not found exception in my Spring MVC web app, I'm using <code>SPRING 4.2.5.RELEASE</code>, I had read several question regarding this topic but the similar questions are using a different spring java configuration.</p>
  
  <p>I have a Global Exception Handler Controller class that have all my Exceptions, this class works fine but I can't handle a 404 page not found exception.</p>
  
  <p>This is the approach that I take following a tutorial</p>
  
  <p>1) I created a class named <code>ResourceNotFoundException</code> that extends from <code>RuntimeException</code> and I putted this annotation over the class definition <code>@ResponseStatus(HttpStatus.NOT_FOUND)</code></p>
  
  <p>like this:</p>
  
  <pre><code>@ResponseStatus(HttpStatus.NOT_FOUND)
  public class ResourceNotFoundException extends RuntimeException { 
  
  }
  </code></pre>
  
  <p>2) I created this method in my exception's controller class</p>
  
  <pre><code>@ExceptionHandler(ResourceNotFoundException.class)
  @ResponseStatus(HttpStatus.NOT_FOUND)
  public String handleResourceNotFoundException() {
  
      return "notFoundJSPPage";
  }
  </code></pre>
  
  <p>But still when I put a URL that doesn't exist I get this error "No mapping found for HTTP request with URI"</p>
  
  <p>The questions that I had read said that I need to enable to true an option for the Dispatcher but since my configuration it's different from the other questions and I don't have a <code>Web.xml</code> I couldn't apply that.</p>
  
  <p>Here it's my Config.java</p>
  
  <pre><code>@EnableWebMvc
  @Configuration
  @ComponentScan({"config", "controllers"})
  public class ConfigMVC extends WebMvcConfigurerAdapter {
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
      }
  
      @Bean
      public UrlBasedViewResolver setupViewResolver() {
          UrlBasedViewResolver resolver = new UrlBasedViewResolver();
          resolver.setPrefix("/WEB-INF/jsp/");
          resolver.setSuffix(".jsp");
          resolver.setViewClass(JstlView.class);
          return resolver;
      }
  
  }
  </code></pre>
  
  <p>Here is my WebInitializer</p>
  
  <pre><code>public class WebInicializar implements WebApplicationInitializer {
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
          ctx.register(ConfigMVC.class);
          ctx.setServletContext(servletContext);
          Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
          servlet.addMapping("/");
          servlet.setLoadOnStartup(1);
  
  
      }
  }
  </code></pre>
  
  <p>Here is my Global Exception Handler Controller</p>
  
  <pre><code>@ControllerAdvice
  public class GlobalExceptionHandlerController {
  
  
      @ExceptionHandler(value = NullPointerException.class)
      public String handleNullPointerException(Exception e) {
  
          System.out.println("A null pointer exception ocurred " + e);
  
          return "nullpointerExceptionPage";
      }
  
  
      @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
      @ExceptionHandler(value = Exception.class)
      public String handleAllException(Exception e) {
  
          System.out.println("A unknow Exception Ocurred: " + e);
  
          return "unknowExceptionPage";
      }
  
  
      @ExceptionHandler(ResourceNotFoundException.class)
      @ResponseStatus(HttpStatus.NOT_FOUND)
      public String handleResourceNotFoundException() {
  
          return "notFoundJSPPage";
      }
  
  }
  </code></pre>
  
  <p>And the class I created that extends Runtime Exception</p>
  
  <pre><code>@ResponseStatus(HttpStatus.NOT_FOUND)
  public class ResourceNotFoundException extends RuntimeException{
  
  }
  </code></pre>