/**
  * Validate the model attribute if applicable.
  *
  * <p>The default implementation checks for {@code @javax.validation.Valid}.
  *
  * @param binder the DataBinder to be used
  * @param parameter the method parameter
  */
 protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
   Annotation[] annotations = parameter.getParameterAnnotations();
   for (Annotation annot : annotations) {
     if (annot.annotationType().getSimpleName().startsWith("Valid")) {
       Object hints = AnnotationUtils.getValue(annot);
       binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
     }
   }
 }
Пример #2
0
  private void doBind(
      WebDataBinder binder, NativeWebRequest webRequest, boolean validate, boolean failOnErrors)
      throws Exception {

    doBind(binder, webRequest);
    if (validate) {
      binder.validate();
    }
    if (failOnErrors && binder.getBindingResult().hasErrors()) {
      throw new BindException(binder.getBindingResult());
    }
  }
 /**
  * Validate the model attribute if applicable.
  *
  * <p>The default implementation checks for {@code @javax.validation.Valid}.
  *
  * @param binder the DataBinder to be used
  * @param parameter the method parameter
  */
 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;
     }
   }
 }
 protected void validateComponent(WebDataBinder binder, MethodParameter parameter)
     throws BindException {
   boolean validateParameter = validateParameter(parameter);
   Annotation[] annotations = binder.getTarget().getClass().getAnnotations();
   for (Annotation annot : annotations) {
     if (annot.annotationType().getSimpleName().startsWith("Valid") && validateParameter) {
       Object hints = AnnotationUtils.getValue(annot);
       binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
     }
   }
   if (binder.getBindingResult().hasErrors()) {
     if (isBindExceptionRequired(binder, parameter)) {
       throw new BindException(binder.getBindingResult());
     }
   }
 }
 private void validate(WebDataBinder binder, MethodParameter parameter)
     throws MethodArgumentNotValidException {
   Annotation[] annotations = parameter.getParameterAnnotations();
   for (Annotation annot : annotations) {
     if (annot.annotationType().getSimpleName().startsWith("Valid")) {
       Object hints = AnnotationUtils.getValue(annot);
       binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
       BindingResult bindingResult = binder.getBindingResult();
       if (bindingResult.hasErrors()) {
         if (isBindingErrorFatal(parameter)) {
           throw new MethodArgumentNotValidException(parameter, bindingResult);
         }
       }
     }
   }
 }
 public Object resolveArgument(
     MethodParameter parameter,
     ModelAndViewContainer mavContainer,
     NativeWebRequest webRequest,
     WebDataBinderFactory binderFactory)
     throws Exception {
   Object arg = readWithMessageConverters(webRequest, parameter, parameter.getParameterType());
   if (shouldValidate(parameter, arg)) {
     String argName = Conventions.getVariableNameForParameter(parameter);
     WebDataBinder binder = binderFactory.createBinder(webRequest, arg, argName);
     binder.validate();
     Errors errors = binder.getBindingResult();
     if (errors.hasErrors()) {
       throw new RequestBodyNotValidException(errors);
     }
   }
   return arg;
 }