JAVAPROGLIB Telegram 6482
🔍 База по Spring Boot

🔹
@Transactional — оборачивает метод в транзакцию

@Transactional
public void saveData(Entity entity) {
repository.save(entity);
}


Автооткат при исключениях, но не ловит checked-исключения без rollbackFor = Exception.class.

🔹 @Async — делает метод асинхронным

@Async
public CompletableFuture<String> fetchData() {
return CompletableFuture.completedFuture("Hello");
}


Использует TaskExecutor, но не забываем про конфиг @EnableAsync.

🔹 @RestControllerAdvice — глобальный хендлер ошибок

@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleException(RuntimeException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
}


Вместо тонны try-catch обрабатываем ошибки централизованно.

🐸 Библиотека джависта #буст
Please open Telegram to view this post
VIEW IN TELEGRAM
👍20🔥32🤔2



tgoop.com/javaproglib/6482
Create:
Last Update:

🔍 База по Spring Boot

🔹
@Transactional — оборачивает метод в транзакцию

@Transactional
public void saveData(Entity entity) {
repository.save(entity);
}


Автооткат при исключениях, но не ловит checked-исключения без rollbackFor = Exception.class.

🔹 @Async — делает метод асинхронным

@Async
public CompletableFuture<String> fetchData() {
return CompletableFuture.completedFuture("Hello");
}


Использует TaskExecutor, но не забываем про конфиг @EnableAsync.

🔹 @RestControllerAdvice — глобальный хендлер ошибок

@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleException(RuntimeException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
}


Вместо тонны try-catch обрабатываем ошибки централизованно.

🐸 Библиотека джависта #буст

BY Библиотека джависта | Java, Spring, Maven, Hibernate




Share with your friend now:
tgoop.com/javaproglib/6482

View MORE
Open in Telegram


Telegram News

Date: |

Informative Telegram desktop app: In the upper left corner, click the Menu icon (the one with three lines). Select “New Channel” from the drop-down menu. With the “Bear Market Screaming Therapy Group,” we’ve now transcended language. Polls Select “New Channel”
from us


Telegram Библиотека джависта | Java, Spring, Maven, Hibernate
FROM American