當前位置: 首頁>>代碼示例>>Java>>正文


Java BeanPropertyBindingResult.hasErrors方法代碼示例

本文整理匯總了Java中org.springframework.validation.BeanPropertyBindingResult.hasErrors方法的典型用法代碼示例。如果您正苦於以下問題:Java BeanPropertyBindingResult.hasErrors方法的具體用法?Java BeanPropertyBindingResult.hasErrors怎麽用?Java BeanPropertyBindingResult.hasErrors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.validation.BeanPropertyBindingResult的用法示例。


在下文中一共展示了BeanPropertyBindingResult.hasErrors方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: validate

import org.springframework.validation.BeanPropertyBindingResult; //導入方法依賴的package包/類
/**
 * 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;
		}
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:35,代碼來源:PayloadArgumentResolver.java

示例2: deserialize

import org.springframework.validation.BeanPropertyBindingResult; //導入方法依賴的package包/類
/**
 * Deserializes the given message.
 * 
 * @param action
 * @return
 * @throws Exception
 */
public T deserialize(WebSocketAction action) throws Exception {
	// first deserialize
	T message = null;
	
	if (messageClass != null) {
		message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);
	}
	
	// then validate
	if (message != null && action.shouldValidatePayload()) {
		SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator);
		
		BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName());
		
		validatorAdapter.validate(message, result);
		
		if (result.hasErrors()) {
			throw new MethodArgumentNotValidException(new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result);
		}
	}
	
	return message;
}
 
開發者ID:Kixeye,項目名稱:chassis,代碼行數:31,代碼來源:RawWebSocketMessage.java

示例3: run

import org.springframework.validation.BeanPropertyBindingResult; //導入方法依賴的package包/類
/**
 * {@link RecordProcessor#run(String)}
 * @param inputFilePath
 * @throws DataImportException
 */
public void run(String inputFilePath) throws DataImportException {
	reader.doBefore(inputFilePath);
	writer.doBefore(this.getTempFilePath(inputFilePath));
	T record = reader.readRecord();
	while (record != null) {
		if (record instanceof DataSetAware) ((DataSetAware) record).setDataSetMetadata(dataSet);
		if (validator != null) {
			BeanPropertyBindingResult bindingResult
					= new BeanPropertyBindingResult(record, record.getClass().getName());
			validator.validate(record, bindingResult);
			if (bindingResult.hasErrors()){
				logger.warn(String.format("Record failed validation: %s", record.toString()));
				if (!options.isSkipInvalidRecords()){
					throw new DataImportException(bindingResult.toString());
				}
			}
		}
		writer.writeRecord(record);
		record = reader.readRecord();
	}
	writer.doAfter();
	reader.doAfter();
	if (importer != null) {
		importer.importFile(this.getTempFilePath(inputFilePath));
	}
}
 
開發者ID:oncoblocks,項目名稱:centromere,代碼行數:32,代碼來源:GenericRecordProcessor.java

示例4: validationTest

import org.springframework.validation.BeanPropertyBindingResult; //導入方法依賴的package包/類
@Test
public void validationTest() throws Exception {
	EntrezGene gene = new EntrezGene();
	BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(gene, gene.getClass().getName());
	validator.validate(gene, bindingResult);
	if (bindingResult.hasErrors()){
		for (ObjectError error: bindingResult.getAllErrors()){
			System.out.println(error.toString());
		}
	} else {
		fail("Validation did not catch missing field.");
	}
}
 
開發者ID:oncoblocks,項目名稱:centromere,代碼行數:14,代碼來源:DataImportTests.java

示例5: validate

import org.springframework.validation.BeanPropertyBindingResult; //導入方法依賴的package包/類
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;
		}
	}
}
 
開發者ID:ralscha,項目名稱:wampspring,代碼行數:32,代碼來源:PayloadArgumentResolver.java

示例6: invoke

import org.springframework.validation.BeanPropertyBindingResult; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
    Object result;
    try {

        //Check method that is being invoked actually even contains the Class we are interested in.
        if (args != null) {
            List<Object> parameterBeansToValidate = null;
            for (int i = 0; i < args.length; i++) {
                if (obj.getEntityClass().isAssignableFrom(args[i].getClass())) {
                    if (parameterBeansToValidate == null)
                        parameterBeansToValidate = new ArrayList<Object>();
                    parameterBeansToValidate.add(args[i]);
                }
            }

            if (parameterBeansToValidate != null && parameterBeansToValidate.size() > 0) {

                //Runs the validator against each bean that was in the parameters.
                for (Object paramBean : parameterBeansToValidate) {

                    if (cachedValidatorsMap.containsKey(paramBean.getClass())) {

                        List<Validator> validators = cachedValidatorsMap.get(paramBean.getClass());

                        for (Validator validator : validators) {
                            if (skip(validator))
                                continue;
                            List<ObjectError> validationErrors = null;
                            if (validator.supports(paramBean.getClass())) {
                                BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult
                                        (paramBean, paramBean.getClass().getName());
                                ValidationUtils.invokeValidator(validator, paramBean, bindingResult);

                                if (bindingResult.hasErrors()) {
                                    if (validationErrors == null)
                                        validationErrors = new ArrayList<ObjectError>();

                                    validationErrors.addAll(bindingResult.getAllErrors());
                                }
                            }

                            if (validationErrors != null && validationErrors.size() > 0) {
                                throw new BeanValidationException(paramBean.getClass().getName() + " " +
                                        "validation failed", validationErrors, paramBean);
                            }
                        }
                    } else {
                        logger.debug("No validators found for Bean - " + paramBean.getClass().getName());
                    }
                }
            }
        }

        logger.debug("Invoking method " + m.getName() + " from dynamic proxy " + getClass().getName());
        result = m.invoke(obj, args);

    } catch (InvocationTargetException e) {
        logger.error("problem with method " + m.getName(), e);
        throw e.getTargetException();
    } finally {
        logger.debug("after method " + m.getName());
    }
    return result;
}
 
開發者ID:makeandbuild,項目名稱:vessl,代碼行數:66,代碼來源:BeanValidationProxy.java


注:本文中的org.springframework.validation.BeanPropertyBindingResult.hasErrors方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。