question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring not throwing NoHandlerFoundException anymore

See original GitHub issue

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?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Harowncommented, May 18, 2017

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.

1reaction
bclozelcommented, Dec 27, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom exception handler for NoHandlerFoundException is ...
Is there an option for custom exception handling without @EnableWebMvc , because it overrides Spring configurations which are declared inside ...
Read more >
Enhance exception handling when building RESTful API with ...
First one sets Spring to throw NoHandlerFoundException exception, which we can easily catch later on to generate a proper response.
Read more >
Spring REST Error Handling Example - Mkyong.com
A controller, if a book id is not found, throws the above BookNotFoundException. BookController.java. package com.mkyong; //.
Read more >
org.springframework.web.servlet.NoHandlerFoundException ...
No handler found -> set appropriate HTTP response status. * @param request current HTTP request * @param response current HTTP response * @throws...
Read more >
Spring Boot Reference Guide
Spring Boot does not require any special tools integration, ... When the application is restarted, the restart classloader is thrown away and a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found