Java Code Examples for org.springframework.validation.annotation.Validated#value()

The following examples show how to use org.springframework.validation.annotation.Validated#value() . 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: PayloadArgumentResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Validate the payload if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link org.springframework.validation.annotation.Validated},
 * and custom annotations whose name starts with "Valid".
 * @param message the currently processed message
 * @param parameter the method parameter
 * @param target the target payload object
 * @throws MethodArgumentNotValidException in case of binding errors
 */
protected void validate(Message<?> message, MethodParameter parameter, Object target) {
	if (this.validator == null) {
		return;
	}
	for (Annotation ann : parameter.getParameterAnnotations()) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			BeanPropertyBindingResult bindingResult =
					new BeanPropertyBindingResult(target, getParameterName(parameter));
			if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
				((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
			}
			else {
				this.validator.validate(target, bindingResult);
			}
			if (bindingResult.hasErrors()) {
				throw new MethodArgumentNotValidException(message, parameter, bindingResult);
			}
			break;
		}
	}
}
 
Example 2
Source File: PayloadArgumentResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Validate the payload if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link org.springframework.validation.annotation.Validated},
 * and custom annotations whose name starts with "Valid".
 * @param message the currently processed message
 * @param parameter the method parameter
 * @param target the target payload object
 * @throws MethodArgumentNotValidException in case of binding errors
 */
protected void validate(Message<?> message, MethodParameter parameter, Object target) {
	if (this.validator == null) {
		return;
	}
	for (Annotation ann : parameter.getParameterAnnotations()) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			BeanPropertyBindingResult bindingResult =
					new BeanPropertyBindingResult(target, getParameterName(parameter));
			if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
				((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
			}
			else {
				this.validator.validate(target, bindingResult);
			}
			if (bindingResult.hasErrors()) {
				throw new MethodArgumentNotValidException(message, parameter, bindingResult);
			}
			break;
		}
	}
}
 
Example 3
Source File: AutoRequestBodyProcessor.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Override
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), ApiController.class)) {
        binder.validate();
    } else {
        Annotation[] annotations = parameter.getParameterAnnotations();
        for (Annotation ann : annotations) {
            Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
            if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
                Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
                Object[] validationHints = (hints instanceof Object[] ? (Object[])hints : new Object[] {hints});
                binder.validate(validationHints);
                break;
            }
        }
    }
}
 
Example 4
Source File: AbstractMessageReaderArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check if the given MethodParameter requires validation and if so return
 * a (possibly empty) Object[] with validation hints. A return value of
 * {@code null} indicates that validation is not required.
 */
@Nullable
private Object[] extractValidationHints(MethodParameter parameter) {
	Annotation[] annotations = parameter.getParameterAnnotations();
	for (Annotation ann : annotations) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			return (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
		}
	}
	return null;
}
 
Example 5
Source File: AbstractMessageConverterMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Validate the request part if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link org.springframework.validation.annotation.Validated},
 * and custom annotations whose name starts with "Valid".
 * @param binder the DataBinder to be used
 * @param methodParam the method parameter
 * @see #isBindExceptionRequired
 * @since 4.1.5
 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter methodParam) {
	Annotation[] annotations = methodParam.getParameterAnnotations();
	for (Annotation ann : annotations) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			binder.validate(validationHints);
			break;
		}
	}
}
 
Example 6
Source File: MethodValidationInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the validation groups to validate against for the given method invocation.
 * <p>Default are the validation groups as specified in the {@link Validated} annotation
 * on the containing target class of the method.
 * @param invocation the current MethodInvocation
 * @return the applicable validation groups as a Class array
 */
protected Class<?>[] determineValidationGroups(MethodInvocation invocation) {
	Validated validatedAnn = AnnotationUtils.findAnnotation(invocation.getMethod(), Validated.class);
	if (validatedAnn == null) {
		validatedAnn = AnnotationUtils.findAnnotation(invocation.getThis().getClass(), Validated.class);
	}
	return (validatedAnn != null ? validatedAnn.value() : new Class<?>[0]);
}
 
Example 7
Source File: CubaMethodValidationInterceptor.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected Class<?>[] determineValidationGroups(MethodInvocation invocation) {
    Validated validatedAnn = AnnotationUtils.findAnnotation(invocation.getMethod(), Validated.class);
    if (validatedAnn == null) {
        validatedAnn = AnnotationUtils.findAnnotation(invocation.getThis().getClass(), Validated.class);
    }
    return (validatedAnn != null ? validatedAnn.value() : new Class<?>[0]);
}
 
Example 8
Source File: AbstractMessageConverterMethodArgumentResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validate the binding target if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link org.springframework.validation.annotation.Validated},
 * and custom annotations whose name starts with "Valid".
 * @param binder the DataBinder to be used
 * @param parameter the method parameter descriptor
 * @since 4.1.5
 * @see #isBindExceptionRequired
 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
	Annotation[] annotations = parameter.getParameterAnnotations();
	for (Annotation ann : annotations) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			binder.validate(validationHints);
			break;
		}
	}
}
 
Example 9
Source File: MethodValidationInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine the validation groups to validate against for the given method invocation.
 * <p>Default are the validation groups as specified in the {@link Validated} annotation
 * on the containing target class of the method.
 * @param invocation the current MethodInvocation
 * @return the applicable validation groups as a Class array
 */
protected Class<?>[] determineValidationGroups(MethodInvocation invocation) {
	Validated validatedAnn = AnnotationUtils.findAnnotation(invocation.getMethod(), Validated.class);
	if (validatedAnn == null) {
		validatedAnn = AnnotationUtils.findAnnotation(invocation.getThis().getClass(), Validated.class);
	}
	return (validatedAnn != null ? validatedAnn.value() : new Class<?>[0]);
}
 
Example 10
Source File: ModelAttributeMethodProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Validate the model attribute if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link org.springframework.validation.annotation.Validated},
 * and custom annotations whose name starts with "Valid".
 * @param binder the DataBinder to be used
 * @param methodParam the method parameter
 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter methodParam) {
	Annotation[] annotations = methodParam.getParameterAnnotations();
	for (Annotation ann : annotations) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			binder.validate(validationHints);
			break;
		}
	}
}
 
Example 11
Source File: AbstractMessageConverterMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Validate the binding target if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link org.springframework.validation.annotation.Validated},
 * and custom annotations whose name starts with "Valid".
 * @param binder the DataBinder to be used
 * @param parameter the method parameter descriptor
 * @since 4.1.5
 * @see #isBindExceptionRequired
 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
	Annotation[] annotations = parameter.getParameterAnnotations();
	for (Annotation ann : annotations) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			binder.validate(validationHints);
			break;
		}
	}
}
 
Example 12
Source File: PayloadMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Validate the payload if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link Validated},
 * and custom annotations whose name starts with "Valid".
 * @param message the currently processed message
 * @param parameter the method parameter
 * @param target the target payload object
 * @throws MethodArgumentNotValidException in case of binding errors
 */
protected void validate(Message<?> message, MethodParameter parameter, Object target) {
	if (this.validator == null) {
		return;
	}
	for (Annotation ann : parameter.getParameterAnnotations()) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			BeanPropertyBindingResult bindingResult =
					new BeanPropertyBindingResult(target, getParameterName(parameter));
			if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
				((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
			}
			else {
				this.validator.validate(target, bindingResult);
			}
			if (bindingResult.hasErrors()) {
				throw new MethodArgumentNotValidException(message, parameter, bindingResult);
			}
			break;
		}
	}
}
 
Example 13
Source File: ModelAttributeMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void validateIfApplicable(WebExchangeDataBinder binder, MethodParameter parameter) {
	for (Annotation ann : parameter.getParameterAnnotations()) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			if (hints != null) {
				Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
				binder.validate(validationHints);
			}
			else {
				binder.validate();
			}
		}
	}
}
 
Example 14
Source File: MethodValidationInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine the validation groups to validate against for the given method invocation.
 * <p>Default are the validation groups as specified in the {@link Validated} annotation
 * on the containing target class of the method.
 * @param invocation the current MethodInvocation
 * @return the applicable validation groups as a Class array
 */
protected Class<?>[] determineValidationGroups(MethodInvocation invocation) {
	Validated validatedAnn = AnnotationUtils.findAnnotation(invocation.getMethod(), Validated.class);
	if (validatedAnn == null) {
		validatedAnn = AnnotationUtils.findAnnotation(invocation.getThis().getClass(), Validated.class);
	}
	return (validatedAnn != null ? validatedAnn.value() : new Class<?>[0]);
}
 
Example 15
Source File: ModelAttributeMethodProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine any validation triggered by the given annotation.
 * @param ann the annotation (potentially a validation annotation)
 * @return the validation hints to apply (possibly an empty array),
 * or {@code null} if this annotation does not trigger any validation
 * @since 5.1
 */
@Nullable
private Object[] determineValidationHints(Annotation ann) {
	Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
	if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
		Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
		if (hints == null) {
			return new Object[0];
		}
		return (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
	}
	return null;
}
 
Example 16
Source File: AbstractMessageConverterMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Validate the binding target if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid},
 * Spring's {@link org.springframework.validation.annotation.Validated},
 * and custom annotations whose name starts with "Valid".
 * @param binder the DataBinder to be used
 * @param parameter the method parameter descriptor
 * @since 4.1.5
 * @see #isBindExceptionRequired
 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
	Annotation[] annotations = parameter.getParameterAnnotations();
	for (Annotation ann : annotations) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			binder.validate(validationHints);
			break;
		}
	}
}
 
Example 17
Source File: AbstractMessageReaderArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Check if the given MethodParameter requires validation and if so return
 * a (possibly empty) Object[] with validation hints. A return value of
 * {@code null} indicates that validation is not required.
 */
@Nullable
private Object[] extractValidationHints(MethodParameter parameter) {
	Annotation[] annotations = parameter.getParameterAnnotations();
	for (Annotation ann : annotations) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			return (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
		}
	}
	return null;
}
 
Example 18
Source File: ModelAttributeMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void validateIfApplicable(WebExchangeDataBinder binder, MethodParameter parameter) {
	for (Annotation ann : parameter.getParameterAnnotations()) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			if (hints != null) {
				Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
				binder.validate(validationHints);
			}
			else {
				binder.validate();
			}
		}
	}
}
 
Example 19
Source File: MethodValidationInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine the validation groups to validate against for the given method invocation.
 * <p>Default are the validation groups as specified in the {@link Validated} annotation
 * on the containing target class of the method.
 * @param invocation the current MethodInvocation
 * @return the applicable validation groups as a Class array
 */
protected Class<?>[] determineValidationGroups(MethodInvocation invocation) {
	Validated validatedAnn = AnnotationUtils.findAnnotation(invocation.getMethod(), Validated.class);
	if (validatedAnn == null) {
		validatedAnn = AnnotationUtils.findAnnotation(invocation.getThis().getClass(), Validated.class);
	}
	return (validatedAnn != null ? validatedAnn.value() : new Class<?>[0]);
}
 
Example 20
Source File: PayloadMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private Consumer<Object> getValidator(Message<?> message, MethodParameter parameter) {
	if (this.validator == null) {
		return null;
	}
	for (Annotation ann : parameter.getParameterAnnotations()) {
		Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
		if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
			Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
			Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
			String name = Conventions.getVariableNameForParameter(parameter);
			return target -> {
				BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(target, name);
				if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
					((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
				}
				else {
					this.validator.validate(target, bindingResult);
				}
				if (bindingResult.hasErrors()) {
					throw new MethodArgumentNotValidException(message, parameter, bindingResult);
				}
			};
		}
	}
	return null;
}