Hi! I think that's an issue. Or am I missing something? :(

I was reading the Spring Reference Guide on how to handle NoHandlerFoundException and found that Spring sets, by default, throwExceptionIfNoHandlerFound to false.

Knowing that, I thought it was a good idea to set this parameter to true.

I'm using Spring Boot.

Did so:

MyConfig.java

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);

        // ...
    }
}

Alright, now throwExceptionIfNoHandlerFound is equals true. However, that didn't worked as expected. The DispatcherServlet continued not throwing the NoHandlerFoundException. This way, I was unable to handle it.

CustomExceptionHandler.java (this didn't worked)

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 <Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        // do my own handle ...

        // then, return ...
    }

}

After a bit of search, found that adding @EnableWebMvc should work. Then, I did add this annotation to my CustomExceptionHandler

CustomExceptionHandler.java (this did worked)

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 <Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        // do my own handle ...

        // then, return ...
    }

}

This way, the handle works. However, I'm using Spring Boot. The Spring Boot docs suggests that if I insert @EnableWebMvc, I would lose some Spring Boot MVC Auto-configuration features, since I would take complete controll of Spring MVC. (See here).

"If you want to take complete control of Spring MVC, you can add your own @Configurationannotated with @EnableWebMvc."

Would I lose Spring MVC Auto-configuration? I mean, the @EnableWebMvc, in my case, it's not in a @Configuration class.

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?

Comment From: philwebb

You should be able to add spring.mvc.throw-exception-if-no-handler-found=true to your application.properties to enable the same feature with the auto-configured DispatcherServlet.

If that's not working for you please try asking on stackoverflow.com and provide a Minimal, Complete, and Verifiable example. Feel free to add a link back here to the stackoverflow question so we can find it.

Comment From: matheuscirillo

StackOverflow question Adding spring.mvc.throw-exception-if-no-handler-found=true without adding @EnableWebMvc like the example above do not work ): I still need to add @EnableWebMvc annotation

Comment From: ilyes12zouaoui

Hello using the following two properties will make spring boot throw NoHandlerFoundException. No need to use @EnableWebMVC with them. spring.mvc.throwExceptionIfNoHandlerFound=true, spring.web.resources.add-mappings=false