本文整理汇总了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;
}
示例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();
}
示例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<ValidationError> 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;
}