spring-boot: Spring not throwing NoHandlerFoundException anymore

After we upgraded from Spring Boot 1.4.0 to 1.5.3 our web application does not throw the NoHandlerFoundException anymore. Configuration looks like the following:

in application.properties:

spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

the corresponding exception controller:

@ControllerAdvice
@EnableWebMvc
public class ExceptionController 
{
       // works
       @ExceptionHandler(AccessDeniedException.class)
	public String handleAccessDeniedException(AccessDeniedException ex, HttpServletRequest request) {
		return "403";
	}

	// doesn't work anymore
	@ExceptionHandler(NoHandlerFoundException.class)
	public String handleNotFoundError(NoHandlerFoundException ex, HttpServletRequest request) {
		return "404";
	}
}

We had no problems with Spring 1.4.0 but it stopped working in 1.5.3. Why is the exception not thrown anymore?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

While preparing a sample application we’ve found the culprit:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
	        "classpath:/META-INF/resources/", "classpath:/resources/",
	        "classpath:/static/", "classpath:/public/" };
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
	    registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
	}
}

Replacing the URl pattern with the subfolder containing the static resources spring.mvc.throw-exception-if-no-handler-found=true have the desired effect. Thank you.

You’re mapping everything with your static path pattern, so this behavior is expected. For further questions, please use StackOverflow.