class RestErrorHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public RestError handleValidationError(MethodArgumentNotValidException ex) {
        LOGGER.info("Handling validation error");

        RestError.Builder error = RestError.getBuilder()
                .status(HttpStatus.BAD_REQUEST)
                .code(HttpStatus.BAD_REQUEST.value())
                .message(ex.getMessage());

        List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();

        for (FieldError fieldError: fieldErrors) {
            error.validationError(fieldError.getField(), fieldError.getCode(), fieldError.getDefaultMessage());
        }

        RestError validationError = error.build();

        LOGGER.info("Returning validation error: {}", validationError);

        return validationError;
    }

}