The issue is likely that the response status isn't being set properly. Using ResponseEntity (which is the recommended approach in Spring Boot 3.x) gives you explicit control over the HTTP status code.
Change your exception handler to:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MyBusinessException.class)
public ResponseEntity<ProblemDetail> handleMyBusinessException(MyBusinessException ex) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.BAD_REQUEST,
ex.getMessage()
);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(problemDetail);
}
}
Why this works:
ResponseEntity explicitly sets the HTTP status code in the response
ProblemDetail is Spring Boot 3.x's built-in support for RFC 7807 (Problem Details for HTTP APIs)
This approach is more reliable than depending on @ResponseStatus annotations
If it still doesn't work, check:
Your @RestControllerAdvice class is in a package scanned by Spring Boot (should be in or under your main application package)
You have the correct imports
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;