Spring Boot Custom Exception Handler
Spring Boot'ta @RestControllerAdvice ile merkezi exception handling. Custom exception sınıfları ve ProblemDetail yanıtları.
By Tolgahan
·
·
344 görüntülenme
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
// 404 - Kaynak bulunamadı
@ExceptionHandler(ResourceNotFoundException.class)
public ProblemDetail handleNotFound(ResourceNotFoundException ex) {
ProblemDetail problem = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, ex.getMessage()
);
problem.setTitle("Kaynak Bulunamadı");
problem.setProperty("errorCode", "RESOURCE_NOT_FOUND");
problem.setProperty("timestamp", Instant.now());
return problem;
}
// 400 - Validation hatası
@ExceptionHandler(MethodArgumentNotValidException.class)
public ProblemDetail handleValidation(MethodArgumentNotValidException ex) {
ProblemDetail problem = ProblemDetail.forStatusAndDetail(
HttpStatus.BAD_REQUEST, "Validasyon hatası"
);
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors()
.forEach(e -> errors.put(e.getField(), e.getDefaultMessage()));
problem.setProperty("fieldErrors", errors);
return problem;
}
// 500 - Beklenmeyen hata
@ExceptionHandler(Exception.class)
public ProblemDetail handleGeneric(Exception ex) {
String correlationId = UUID.randomUUID().toString();
log.error("Unexpected error [{}]: {}", correlationId, ex.getMessage(), ex);
ProblemDetail problem = ProblemDetail.forStatusAndDetail(
HttpStatus.INTERNAL_SERVER_ERROR, "Beklenmeyen bir hata oluştu"
);
problem.setProperty("correlationId", correlationId);
return problem;
}
}
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
// 404 - Kaynak bulunamadı
@ExceptionHandler(ResourceNotFoundException.class)
public ProblemDetail handleNotFound(ResourceNotFoundException ex) {
ProblemDetail problem = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, ex.getMessage()
);
problem.setTitle("Kaynak Bulunamadı");
problem.setProperty("errorCode", "RESOURCE_NOT_FOUND");
problem.setProperty("timestamp", Instant.now());
return problem;
}
// 400 - Validation hatası
@ExceptionHandler(MethodArgumentNotValidException.class)
public ProblemDetail handleValidation(MethodArgumentNotValidException ex) {
ProblemDetail problem = ProblemDetail.forStatusAndDetail(
HttpStatus.BAD_REQUEST, "Validasyon hatası"
);
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors()
.forEach(e -> errors.put(e.getField(), e.getDefaultMessage()));
problem.setProperty("fieldErrors", errors);
return problem;
}
// 500 - Beklenmeyen hata
@ExceptionHandler(Exception.class)
public ProblemDetail handleGeneric(Exception ex) {
String correlationId = UUID.randomUUID().toString();
log.error("Unexpected error [{}]: {}", correlationId, ex.getMessage(), ex);
ProblemDetail problem = ProblemDetail.forStatusAndDetail(
HttpStatus.INTERNAL_SERVER_ERROR, "Beklenmeyen bir hata oluştu"
);
problem.setProperty("correlationId", correlationId);
return problem;
}
}
AI Asistan
Sorularını yanıtlamaya hazır