Vamos a crear excepciones globales para todos los controladores primero creamos el paquete
com.tutosoftware.aloja.exceptionhandler y creamos la clase GlobalExceptionHandler
package com.tutosoftware.aloja.exceptionhandler;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> handlerArgumentException(IllegalArgumentException ex){
return new ResponseEntity<String>(ex.getMessage(),HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handlerRuntimeException(RuntimeException ex)
{
return new ResponseEntity<String>(ex.getMessage(),HttpStatus.BAD_GATEWAY);
}
}