Java Code Examples for org.springframework.validation.FieldError#getRejectedValue()

The following examples show how to use org.springframework.validation.FieldError#getRejectedValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EscapedErrors.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
private <T extends ObjectError> T escapeObjectError(@Nullable T source) {
	if (source == null) {
		return null;
	}
	String defaultMessage = source.getDefaultMessage();
	if (defaultMessage != null) {
		defaultMessage = HtmlUtils.htmlEscape(defaultMessage);
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value, fieldError.isBindingFailure(),
				fieldError.getCodes(), fieldError.getArguments(), defaultMessage);
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(), defaultMessage);
	}
}
 
Example 2
Source File: EscapedErrors.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
private <T extends ObjectError> T escapeObjectError(@Nullable T source) {
	if (source == null) {
		return null;
	}
	String defaultMessage = source.getDefaultMessage();
	if (defaultMessage != null) {
		defaultMessage = HtmlUtils.htmlEscape(defaultMessage);
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value, fieldError.isBindingFailure(),
				fieldError.getCodes(), fieldError.getArguments(), defaultMessage);
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(), defaultMessage);
	}
}
 
Example 3
Source File: PetControllerExceptionHandler.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * The handleMethodArgumentNotValid method creates a response with a list of messages that contains the fields with errors
 *
 * @param exception MethodArgumentNotValidException
 * @return 400 and a list of messages with invalid fields
 */
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiMessageView> handleMethodArgumentNotValid(MethodArgumentNotValidException exception) {
    List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
    List<Object[]> messages = new ArrayList<>();

    for (FieldError fieldError : fieldErrors) {
        Object[] messageFields = new Object[3];
        messageFields[0] = fieldError.getField();
        messageFields[1] = fieldError.getRejectedValue();
        messageFields[2] = fieldError.getDefaultMessage();
        messages.add(messageFields);
    }

    List<ApiMessage> listApiMessage = messageService
        .createMessage("org.zowe.apiml.sampleservice.api.petMethodArgumentNotValid", messages)
        .stream()
        .map(Message::mapToApiMessage)
        .collect(Collectors.toList());

    return ResponseEntity
        .status(HttpStatus.BAD_REQUEST)
        .contentType(MediaType.APPLICATION_JSON_UTF8)
        .body(new ApiMessageView(listApiMessage));
}
 
Example 4
Source File: EscapedErrors.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends ObjectError> T escapeObjectError(T source) {
	if (source == null) {
		return null;
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value,
				fieldError.isBindingFailure(), fieldError.getCodes(),
				fieldError.getArguments(), HtmlUtils.htmlEscape(fieldError.getDefaultMessage()));
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(),
				HtmlUtils.htmlEscape(source.getDefaultMessage()));
	}
}
 
Example 5
Source File: ErrorHalRepresentationFactory.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
private String serializeRejectedValue(FieldError e) {
    if (e.getRejectedValue() == null) {
        return "null";
    }

    if (e.getRejectedValue() instanceof String) {
        return (String) e.getRejectedValue();
    } else {
        try {
            return objectMapper.writeValueAsString(e.getRejectedValue());
        } catch (JsonProcessingException ignore) {
            return e.getRejectedValue().toString();
        }
    }

}
 
Example 6
Source File: UniformHandler.java    From Milkomeda with MIT License 5 votes vote down vote up
/**
 * 处理Bean校验异常
 * @param ex            异常
 * @param bindingResult 错误绑定数据
 * @return  ResponseEntity
 */
private ResponseEntity<Object> handleValidBeanExceptionResponse(Exception ex, BindingResult bindingResult) {
    ObjectError objectError = bindingResult.getAllErrors().get(0);
    String message = objectError.getDefaultMessage();
    if (objectError.getArguments() != null && objectError.getArguments().length > 0) {
        FieldError fieldError = (FieldError) objectError;
        message = WebContext.getRequest().getRequestURI() + " [" + fieldError.getField() + "=" + fieldError.getRejectedValue() + "] " + message;
    }
    log.warn("Hydrogen uniform valid response exception with msg: {} ", message);
    return handleExceptionResponse(ex, HttpStatus.BAD_REQUEST.value(), message);
}
 
Example 7
Source File: ControllerExceptionHandler.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@ExceptionHandler(BindException.class)
public String onBindException(final BindException e, final Popups popups) {
    final FieldError fieldError = e.getFieldError();
    if (fieldError != null && fieldError.getRejectedValue() != null) {
        popups.alert("GWUA.error.invalidInputValue", e.getFieldError().getRejectedValue());
    } else {
        popups.alert("Error");
    }
    return "messages";
}
 
Example 8
Source File: Message.java    From sdk-rest with MIT License 5 votes vote down vote up
public Message(FieldError fieldError) {
	super();
	this.detailMessage = fieldError.getDefaultMessage() + ". REJECTED VALUE = " + fieldError.getRejectedValue();
	this.propertyName = fieldError.getField();
	this.severity = "ERROR";
	this.type = "VALIDATION_ERROR";
}
 
Example 9
Source File: ProblemExceptionResponseGenerator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Problem.Error createProblemError(FieldError fieldError) {
  Error.Builder builder = Error.builder();

  buildProblemError(fieldError, builder);
  builder.setField(fieldError.getField());

  Object rejectedValue = fieldError.getRejectedValue();
  if (rejectedValue != null) {
    builder.setValue(rejectedValue.toString());
  }

  buildProblemError(fieldError, builder);

  return builder.build();
}
 
Example 10
Source File: OriginTrackedFieldError.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private OriginTrackedFieldError(FieldError fieldError) {
    super(fieldError.getObjectName(), fieldError.getField(),
            fieldError.getRejectedValue(), fieldError.isBindingFailure(),
            fieldError.getCodes(), fieldError.getArguments(),
            fieldError.getDefaultMessage());
}
 
Example 11
Source File: FormErrorRepresentation.java    From abixen-platform with GNU Lesser General Public License v2.1 4 votes vote down vote up
public FormErrorRepresentation(FieldError fieldError) {
    this.field = fieldError.getField();
    this.code = fieldError.getCode();
    this.message = fieldError.getDefaultMessage();
    this.rejectedValue = fieldError.getRejectedValue();
}
 
Example 12
Source File: EscapedErrors.java    From spring4-understanding with Apache License 2.0 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends ObjectError> T escapeObjectError(T source) {
	if (source == null) {
		return null;
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value,
				fieldError.isBindingFailure(), fieldError.getCodes(),
				fieldError.getArguments(), HtmlUtils.htmlEscape(fieldError.getDefaultMessage()));
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(),
				HtmlUtils.htmlEscape(source.getDefaultMessage()));
	}
}