0

I recently upgraded to Spring Boot 3.5 (Jakarta packages) and noticed my global exception handler isn’t being triggered.

@RestController
public class UserController {
    @GetMapping("/test")
    public String test() {
        throw new MyBusinessException("Something went wrong");
    }
}

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MyBusinessException.class)
    public ProblemDetail handleMyBusinessException(MyBusinessException ex) {
        return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage());
    }
}

public class MyBusinessException extends RuntimeException {
    public MyBusinessException(String message) { super(message); }
}

When I call /test, I still get the default 500 response instead of my custom ProblemDetail.

I’ve verified:

  • Packages are under the same base package.

  • Using jakarta.* imports.

  • No custom filters or resolvers.

Question: Why isn’t my @RestControllerAdvice handler being called in Spring Boot 3.x, and how can I fix it so it returns my custom JSON response?

2
  • how are you triggering UserController::test method? in test or via postman/curl? If in test, show your test code please Commented Oct 27 at 7:41
  • Have you enabled the problem detail support? Commented Oct 28 at 10:00

1 Answer 1

-2
@RestControllerAdvice
@Component // Explicitly mark as a component
public class GlobalExceptionHandler {
    // ... your handler method
}

The most likely fix is ensuring proper package scanning or explicitly marking GlobalExceptionHandler as a component. After addressing these, your @RestControllerAdvice should catch MyBusinessException and return the custom ProblemDetail

Sign up to request clarification or add additional context in comments.

1 Comment

@RestControllerAdvice is already an @Component adding the annotation won't solve a thing only will add clutter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.