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


Java ConstraintViolation.getMessageTemplate方法代碼示例

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


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

示例1: toCode

import javax.validation.ConstraintViolation; //導入方法依賴的package包/類
private String toCode(ConstraintViolation<?> violation) {
	if (violation.getConstraintDescriptor() != null) {
		Annotation annotation = violation.getConstraintDescriptor().getAnnotation();

		if (annotation != null) {
			Class<?> clazz = annotation.getClass();
			Class<?> superclass = annotation.getClass().getSuperclass();
			Class<?>[] interfaces = annotation.getClass().getInterfaces();
			if (superclass == Proxy.class && interfaces.length == 1) {
				clazz = interfaces[0];
			}

			return clazz.getName();
		}
	}
	if (violation.getMessageTemplate() != null) {
		return violation.getMessageTemplate().replace("{", "").replaceAll("}", "");
	}
	return null;
}
 
開發者ID:crnk-project,項目名稱:crnk-framework,代碼行數:21,代碼來源:ConstraintViolationExceptionMapper.java

示例2: toErrorResponse

import javax.validation.ConstraintViolation; //導入方法依賴的package包/類
@Override
public ErrorResponse toErrorResponse(ConstraintViolationException cve) {
	LOGGER.warn("a ConstraintViolationException occured", cve);

	List<ErrorData> errors = new ArrayList<>();
	for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {

		ErrorDataBuilder builder = ErrorData.builder();
		builder = builder.addMetaField(META_TYPE_KEY, META_TYPE_VALUE);
		builder = builder.setStatus(String.valueOf(HttpStatus.UNPROCESSABLE_ENTITY_422));
		builder = builder.setDetail(violation.getMessage());

		builder = builder.setCode(toCode(violation));
		if (violation.getMessageTemplate() != null) {
			builder = builder.addMetaField(META_MESSAGE_TEMPLATE, violation.getMessageTemplate());
		}

		// for now we just provide root resource validation information
		// depending on bulk update spec, we might also provide the leaf information in the future
		if (violation.getRootBean() != null) {
			ResourceRef resourceRef = resolvePath(violation);
			builder = builder.addMetaField(META_RESOURCE_ID, resourceRef.getRootResourceId());
			builder = builder.addMetaField(META_RESOURCE_TYPE, resourceRef.getRootResourceType());
			builder = builder.setSourcePointer(resourceRef.getRootSourcePointer());
		}

		ErrorData error = builder.build();
		errors.add(error);
	}

	return ErrorResponse.builder().setStatus(HttpStatus.UNPROCESSABLE_ENTITY_422).setErrorData(errors).build();
}
 
開發者ID:crnk-project,項目名稱:crnk-framework,代碼行數:33,代碼來源:ConstraintViolationExceptionMapper.java

示例3: contOnValidationError

import javax.validation.ConstraintViolation; //導入方法依賴的package包/類
/**
 * Accepts the result from one of the many validation methods available and
 * returns a List of ValidationErrors. If the size of the List is 0, no errors
 * were encounter during validation.
 *
 * Usage:
 * <pre>
 *     Validator validator = getValidator();
 *     List&lt;ValidationError&gt; errors = contOnValidationError(
 *         validator.validateProperty(myObject, "uuid"),
 *         validator.validateProperty(myObject, "name")
 *      );
 *      // If validation fails, this line will be reached.
 * </pre>
 *
 * @param violationsArray a Set of one or more ConstraintViolations
 * @return a List of zero or more ValidationErrors
 * @since 1.0.0
 */
@SafeVarargs
protected final List<ValidationError> contOnValidationError(final Set<ConstraintViolation<Object>>... violationsArray) {
    final List<ValidationError> errors = new ArrayList<>();
    for (Set<ConstraintViolation<Object>> violations : violationsArray) {
        for (ConstraintViolation violation : violations) {
            if (violation.getPropertyPath().iterator().next().getName() != null) {
                final String path = violation.getPropertyPath() != null ? violation.getPropertyPath().toString() : null;
                final String message = violation.getMessage() != null ? StringUtils.removeStart(violation.getMessage(), path + ".") : null;
                final String messageTemplate = violation.getMessageTemplate();
                final String invalidValue = violation.getInvalidValue() != null ? violation.getInvalidValue().toString() : null;
                final ValidationError error = new ValidationError(message, messageTemplate, path, invalidValue);
                errors.add(error);
            }
        }
    }
    return errors;
}
 
開發者ID:stevespringett,項目名稱:Alpine,代碼行數:37,代碼來源:AlpineResource.java


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