Since spring-boot 2.6.0, @ExceptionHandler(NoHandlerFoundException.class) in a class annotated with @ControllerAdvice is not working any more. The same code was working properly with spring-boot 2.5.7.
Steps to reproduce
Set the version of spring-boot to 2.5.7 in build.gradle
Run ./gradlew bootRun
Go to http://localhost:8080/test/hell => you get message formatted by the exception handler :+1:
Stop the application
Set the version of spring-boot to 2.6.0 in build.gradle
Run ./gradlew bootRun
Go to http://localhost:8080/test/hell => you get the standard error page :-1:
Code
I will provide a complete test case reproducing the problem, but main files are pasted below
ControllerAdvisor.java
@ControllerAdvice
public class ControllerAdvisor {
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Object> handle(final NoHandlerFoundException e) {
System.err.println("handleNoHandlerFoundException: " + e.getMessage());
return new ResponseEntity<>(
Map.ofEntries(
Map.entry("timestamp", LocalDateTime.now()),
Map.entry("message", "Page not found " + e.getRequestURL())),
HttpStatus.BAD_REQUEST);
}
}
MyController.java
@RestController
@RequestMapping("/test")
public class MyController {
@GetMapping("/hello/{text}")
public String hello(@PathVariable("value") final String text) {
return String.format("Greetings %s from Spring Boot", text);
}
}
Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
build.gradle
plugins {
id 'org.springframework.boot' version '2.5.7'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
Comment From: nvervelle
Complete project showing the different behavior between spring-boot 2.5.7 and spring-boot 2.6.0
Comment From: wilkinsona
Thanks for the sample.
spring.resources.add-mappings has been deprecated since 2.4 and support for it was removed in 2.6.0. You should use spring.web.resources.add-mappings with Spring Boot 2.4 and later. With this change to the property name, the sample works with both 2.5.7 and 2.6.0.
If you're using an IDE that supports Spring Boot's configuration property metadata, the deprecated property should be highlighted as an error when editing the application.properties file due to these changes made in 2.6.0.M1.
Comment From: mclouvem
Hi,
I'm facing same issue here, but using version 2.6.2. NoHandlerFoundException is never throwed. I have set the correct property inside the application.yaml file spring.web.resources.add-mappings: false
Is there some issue with version 2.6.2 ???
Comment From: wilkinsona
Not that we're aware of. If you'd like us to investigate, please open a new issue with a minimal sample that reproduces the problem.