@@ -0,0 +1,2280 @@
+package quanlynhansu.config;%0A%0Aimport javax.validation.ConstraintViolationException;%0A%0Aimport org.slf4j.Logger;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.dao.OptimisticLockingFailureException;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0Aimport org.springframework.http.ResponseEntity.BodyBuilder;%0Aimport org.springframework.web.bind.annotation.ControllerAdvice;%0Aimport org.springframework.web.bind.annotation.ExceptionHandler;%0A%0Aimport com.fasterxml.jackson.core.JsonProcessingException;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0A%0A@ControllerAdvice%0Apublic class ExceptionHandlingController %7B%0A%09private static final Logger LOGGER = org.slf4j.LoggerFactory%0A%09%09%09.getLogger(ExceptionHandlingController.class);%0A%0A%09private static ObjectMapper objectMapper;%0A%0A%09@Autowired%0A%09public void setObjectMapper(ObjectMapper objectMapper) %7B%0A%09%09ExceptionHandlingController.objectMapper = objectMapper;%0A%09%7D%0A%0A%09@ExceptionHandler(OptimisticLockingFailureException.class)%0A%09public ResponseEntity%3C?%3E handleConcurrentUpdate(Exception exception) %7B%0A%09%09return log(ResponseEntity.status(HttpStatus.PRECONDITION_FAILED),%0A%09%09%09%09exception)%0A%09%09%09%09.body(errorFor(%22B%E1%BA%A1n %C4%91ang c%E1%BB%91 g%E1%BA%AFn s%E1%BB%ADa m%E1%BB%99t d%E1%BB%AF li%E1%BB%87u c%C3%A1i m%C3%A0 v%E1%BB%ABa %C4%91%C6%B0%E1%BB%A3c t%C3%B9y ch%E1%BB%89nh b%E1%BB%9Fi m%E1%BB%99t admin kh%C3%A1c, load l%E1%BA%A1i trang n%E1%BA%BFu b%E1%BA%A1n ti%E1%BA%BFp t%E1%BB%A5c mu%E1%BB%91n s%E1%BB%ADa d%E1%BB%AF li%E1%BB%87u n%C3%A0y.%22));%0A%09%7D%0A%0A%09@ExceptionHandler(ConstraintViolationException.class)%0A%09public ResponseEntity%3C?%3E handleValidationError(Exception exception) %7B%0A%09%09return log(ResponseEntity.status(HttpStatus.BAD_REQUEST), exception)%0A%09%09%09%09.body(errorFor(%22Data validation error occurred%22));%0A%09%7D%0A%0A%09@ExceptionHandler(Throwable.class)%0A%09public ResponseEntity%3C?%3E handleUnexpectedException(Throwable exception) %7B%0A%09%09return log(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR),%0A%09%09%09%09exception).body(errorFor(%22Internal server error occurred%22));%0A%09%7D%0A%0A%09private BodyBuilder log(BodyBuilder bb, Throwable exception) %7B%0A%09%09// Enhancement of the body can happen here.%0A%09%09LOGGER.error(%22Unhandled exception occurred%22, exception);%0A%09%09return bb;%0A%09%7D%0A%0A%09public static String errorFor(String message) %7B%0A%09%09try %7B%0A%09%09%09return objectMapper.writeValueAsString(message);%0A%09%09%7D catch (JsonProcessingException e) %7B%0A%09%09%09throw new RuntimeException(%22Error during error serialization%22, e);%0A%09%09%7D%0A%09%7D%0A%7D%0A

Add class ExceptionController