S'il n'y a aucun chemin correspondant au mappage de servlet dans spring-mvc, l'avertissement suivant apparaîtra dans le journal. Dans ce cas, lancez une exception côté ressort et attrapez-la. Cela gère 404 (en utilisant spring au lieu de web.xml).
[org.springframework.web.servlet.PageNotFound](default task-2) No mapping found for HTTP request with URI [/xxxx] in DispatcherServlet with name 'dispatcher'
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebMVCApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
	@Override
	protected Class<?>[] getRootConfigClasses() {
		Class<?>[] configurations = {AppConfiguration.class};
		return configurations;
	}
	@Override
	protected Class<?>[] getServletConfigClasses() {
		Class<?>[] configs = {};
		return configs;
	}
	@Override
	protected String[] getServletMappings() {
		String[] mappings = {"/"};
		return mappings;
	}
	@Override
	protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
		DispatcherServlet dispatcherServlet = (DispatcherServlet)super.createDispatcherServlet(servletAppContext);
		dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
		return dispatcherServlet;
	}
}
En définissant setThrowExceptionIfNoHandlerFound sur true, une exception sera levée si le gestionnaire correspondant à la demande est introuvable.
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
@RestControllerAdvice
public class NoHandlerFoundControllerAdvice {
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseEntity<String> handleNoHandlerFoundException(NoHandlerFoundException ex) {
    	return new ResponseEntity<>("not found", HttpStatus.NOT_FOUND);
    }
}
Créez une classe qui affecte ControllerAdvice (ou ControllerAdvice) pour décrire la gestion des exceptions commune à plusieurs contrôleurs. Puisque spring a été défini précédemment pour lancer NoHandlerFoundException s'il n'y a pas de gestionnaire correspondant au chemin, spécifiez-le dans ExceptionHandler.