A controller-advice selector can constrain a global exception handler to specified controller types.
Spring @RestControllerAdvice selector: apply an error policy to one controller family
One error policy need not cover every route
The checked fixture gives ScopedReceiptController a 422 response for invalid input, while a second controller using the same exception receives the fallback 500 response. assignableTypes selects the first controller family, and @Order places the scoped advice ahead of the fallback. This is a deliberate policy boundary, not a way to conceal inconsistent API designs.
Advice matching is resolved around controller dispatch. A controller-local handler still takes priority for its own exception. Local precedence explains that first step; public error shape covers the next one.
Make new controllers prove their policy
A new controller outside the selector can unexpectedly fall through to a generic response. Test one selected and one unselected controller, then run a web-context test that checks actual component registration. The fixture is standalone MockMvc and explicitly installs both advice objects.
Checked code
@RestControllerAdvice(assignableTypes = ScopedReceiptController.class)
@Order(10)
static class ScopedReceiptAdvice {
@ExceptionHandler(IllegalArgumentException.class)
ResponseEntity<String> rejected(IllegalArgumentException invalid) {
return ResponseEntity.unprocessableEntity().body("invalid scoped receipt");
}
}Verification boundary
The Maven source kit passes LifecycleAndMvcDispatchContractTest.adviceSelectorOnlyHandlesItsAssignedController on its local Spring Boot 4 and Java 21 fixture.
Cost and ownership
Advice selectors add matching work at request time; the fixture has two controllers and does not measure throughput. Broad selector policies can be hard to audit when many controllers are added, so keep the scope and fallback behavior in an integration test.
Common Mistakes
- Do not assume an advice selector covers controllers introduced later.
- Do not mistake @Order for authorization or handler mapping order.
- Do not claim standalone MockMvc proves production advice scanning.
Read next
Spring MVC exception handlers: a controller-local handler wins before global advice, Spring MVC ProblemDetail: stable errors without leaking internals, Spring MVC request lifecycle: from servlet filter to response body, Spring MockMvc standalone tests: know which HTTP layers were assembled.
