I have a problem, as the title suggests my method annotated with @ExceptionHandler is not invoked when exceptions are thrown. I've tried using as an argument of @ExceptionHandler(...) annotation both specific exceptions as well as their superclasses. I've tried debugging it and put a breakpoint inside handleExceptions() method annotated with @ExceptionHandler but it didn't go inside. I thought that maybe spring context does not see my GlobalExceptionHandler class as a spring bean and checked the logs in debug mode and GlobalExceptionHandler class is registered as a spring bean: 2024-03-06 11:54:29.123 DEBUG 10196 --- [main] o.s.b.f.s.DefaultListableBeanFactory: Creating shared instance of singleton bean 'globalExceptionHandler'. I don't have the faintest idea what the problem is, thanks in advance:
Project tree:
Main class:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = {"pl.esp", "pl.o4b"})
public class DocumentServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DocumentServiceApplication.class, args);
}
}
ControllerAdvice class
@RestControllerAdvice
@Slf4j
//tried annotating it with @Component even though @RestController has this annotation within itself, but it did'nt work
public class GlobalExceptionHandler {
@ExceptionHandler(PublishDocumentException.class)
public ResponseEntity<ErrorResponse> handleExceptions(final PublishDocumentException ex) {
log.info("Exception thrown {}", ex.getClass().getName());
final ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setDescription(String.valueOf(ex.getCause()));
errorResponse.setStatusCode(HttpStatus.valueOf(ex.code()));
errorResponse.setMessage(ex.getMessage());
return new ResponseEntity<>(errorResponse, errorResponse.getStatusCode());
}
}
ErrorResponse Class
@Getter
@Setter
//tried annotating it with @Component too
public class ErrorResponse {
private String description;
private HttpStatus statusCode;
private String message;
private String reference;
}
method throwing exception
@ApplicationService
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Slf4j
public class DocumentServiceImpl{
public PublishDocumentResponse publishDocument(final String sessionId) {
//...logic//
throw new PublishDocumentException(FAILED_TO_GENERATE_PDF_DOCUMENT);
}
}
PublishException class
public class PublishDocumentException extends HttpRuntimeException {
public PublishDocumentException(final String body) {
super(HttpStatus.SC_BAD_REQUEST, body);
}
}
HttpRunTimeException class
public class HttpRuntimeException extends GeneralRuntimeException implements MessageException<Integer, String, String> {
public HttpRuntimeException(int httpStatus, String body) {
super(httpStatus, body);
}
public HttpRuntimeException(int httpStatus, String body, Object... params) {
super(httpStatus, body, params);
}
}
Controller method:
@RestController
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Slf4j
public class DocumentController {
private final DocumentService documentServiceImpl;
@GetMapping(value = "/documents/declarations/generate",
produces = APPLICATION_JSON_VALUE)
public ResponseEntity<PublishDocumentResponse> generateDocument(@RequestParam final String sessionId) {
var response = documentServiceImpl.generateDocument(sessionId);
log.info("Document generated successfully! Responding status: {}!", HttpStatus.OK);
return ResponseEntity.ok(response);
}
}
