Spring @ControllerAdvice

@ControllerAdvice(annotations = Controller.class)
public class ControllerErrorAdvice extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = { IllegalArgumentException.class, IllegalStateException.class,
        NullPointerException.class })
protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest request) {
    return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), HttpStatus.CONFLICT, request);
}

}

http://www.baeldung.com/exception-handling-for-rest-with-spring
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-controller-advice