本文整理汇总了Java中javax.validation.Path.Node方法的典型用法代码示例。如果您正苦于以下问题:Java Path.Node方法的具体用法?Java Path.Node怎么用?Java Path.Node使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.validation.Path
的用法示例。
在下文中一共展示了Path.Node方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getStringBasedPath
import javax.validation.Path; //导入方法依赖的package包/类
private String getStringBasedPath(Path.Node traversableProperty, Path pathToTraversableObject) {
StringBuilder path = new StringBuilder( );
for ( Path.Node node : pathToTraversableObject ) {
if (node.getName() != null) {
path.append( node.getName() ).append( "." );
}
}
if ( traversableProperty.getName() == null ) {
throw new AssertionFailure(
"TraversableResolver being passed a traversableProperty with null name. pathToTraversableObject: "
+ path.toString() );
}
path.append( traversableProperty.getName() );
return path.toString();
}
示例2: getValidationErrorResponse
import javax.validation.Path; //导入方法依赖的package包/类
private List<ValidationMessage> getValidationErrorResponse(ConstraintViolationException constraintViolationException) {
final List<ValidationMessage> validationErrors = new ArrayList<>();
LOG.error("Got validation errors", constraintViolationException);
for (ConstraintViolation<?> violationSet : constraintViolationException.getConstraintViolations()) {
List<String> propertyList = new ArrayList<>();
Iterator<Path.Node> propertyIterator = violationSet
.getPropertyPath().iterator();
while (propertyIterator.hasNext()) {
propertyList.add(propertyIterator.next().getName());
}
// add violations errors in response
validationErrors.add(ValidationMessage.builder()
.entity(violationSet.getRootBeanClass().getName())
// remove { and }
.messageTemplate(violationSet.getMessageTemplate().replaceAll("^[{]|[}]$", ""))
.propertyList(propertyList).build());
}
return validationErrors;
}
示例3: convert
import javax.validation.Path; //导入方法依赖的package包/类
/**
* @see org.tools.hqlbuilder.common.interfaces.ValidationExceptionConverter#convert(java.lang.Exception)
*/
@Override
public ValidationException convert(Exception e) {
javax.validation.ConstraintViolationException ex = (ConstraintViolationException) e;
List<org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue> ivs = new ArrayList<org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue>();
for (javax.validation.ConstraintViolation<?> iv : ex.getConstraintViolations()) {
Object bean = iv.getLeafBean();
Class<?> beanClass = iv.getRootBeanClass();
String message = iv.getMessage();
Path path = iv.getPropertyPath();
Iterator<javax.validation.Path.Node> it = path.iterator();
Path.Node node = it.next();
while (it.hasNext()) {
node = it.next();
}
String propertyName = String.valueOf(node);
String propertyPath = String.valueOf(path);
Object rootBean = iv.getRootBean();
Object value = iv.getInvalidValue();
org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue tmp = new org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue(
bean, beanClass, message, propertyName, propertyPath, rootBean, value);
ivs.add(tmp);
}
return new ValidationException(ex.getMessage(), ivs);
}
示例4: convert
import javax.validation.Path; //导入方法依赖的package包/类
/**
* @see org.tools.hqlbuilder.common.interfaces.ValidationExceptionConverter#convert(java.lang.Exception)
*/
@Override
public ValidationException convert(Exception e) {
javax.validation.ConstraintViolationException ex = (ConstraintViolationException) e;
List<org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue> ivs = new ArrayList<>();
for (javax.validation.ConstraintViolation<?> iv : ex.getConstraintViolations()) {
Object bean = iv.getLeafBean();
Class<?> beanClass = iv.getRootBeanClass();
String message = iv.getMessage();
Path path = iv.getPropertyPath();
Iterator<javax.validation.Path.Node> it = path.iterator();
Path.Node node = it.next();
while (it.hasNext()) {
node = it.next();
}
String propertyName = String.valueOf(node);
String propertyPath = String.valueOf(path);
Object rootBean = iv.getRootBean();
Object value = iv.getInvalidValue();
org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue tmp = new org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue(
bean, beanClass, message, propertyName, propertyPath, rootBean, value);
ivs.add(tmp);
}
return new ValidationException(ex.getMessage(), ivs);
}
示例5: testExtractViolationInfoFromNode
import javax.validation.Path; //导入方法依赖的package包/类
/**
* Test of extractViolationInfoFromNode method, of class ConstraintServices.
*/
@Test
public void testExtractViolationInfoFromNode() {
System.out.println("extractViolationInfoFromNode");
Path.Node node = mock(Path.Node.class);
when(node.getKind()).thenReturn(ElementKind.METHOD).thenReturn(ElementKind.PARAMETER).thenReturn(ElementKind.PROPERTY);
when(node.getName()).thenReturn("arg0").thenReturn("prop");
doReturn(5).when(instance).getIndexFromArgname(anyString());
ConstraintViolation cv = new ConstraintViolation();
instance.extractViolationInfoFromNode(node, cv);
assertThat(cv.getMessage()).isNull();
assertThat(cv.getIndex()).isEqualTo(0);
assertThat(cv.getProp()).isNull();
cv = new ConstraintViolation();
instance.extractViolationInfoFromNode(node, cv);
assertThat(cv.getMessage()).isNull();
assertThat(cv.getIndex()).isEqualTo(5);
assertThat(cv.getProp()).isNull();
cv = new ConstraintViolation();
instance.extractViolationInfoFromNode(node, cv);
assertThat(cv.getMessage()).isNull();
assertThat(cv.getIndex()).isEqualTo(0);
assertThat(cv.getProp()).isEqualTo("prop");
}
示例6: as
import javax.validation.Path; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T extends Path.Node> T as(final Class<T> nodeType) throws ClassCastException { // NOPMD
if (this.kind == ElementKind.BEAN && nodeType == BeanNode.class
|| this.kind == ElementKind.CONSTRUCTOR && nodeType == ConstructorNode.class
|| this.kind == ElementKind.CROSS_PARAMETER && nodeType == CrossParameterNode.class
|| this.kind == ElementKind.METHOD && nodeType == MethodNode.class
|| this.kind == ElementKind.PARAMETER && nodeType == ParameterNode.class
|| this.kind == ElementKind.PROPERTY && (nodeType == PropertyNode.class
|| nodeType == org.hibernate.validator.path.PropertyNode.class)
|| this.kind == ElementKind.RETURN_VALUE && nodeType == ReturnValueNode.class
|| this.kind == ElementKind.CONTAINER_ELEMENT && (nodeType == ContainerElementNode.class
|| nodeType == org.hibernate.validator.path.ContainerElementNode.class)) {
return (T) this;
}
throw LOG.getUnableToNarrowNodeTypeException(this.getClass(), this.kind, nodeType);
}
示例7: getResponseStatus
import javax.validation.Path; //导入方法依赖的package包/类
/**
* Determine the response status (400 or 500) from the given BV exception.
*
* @param violation BV exception.
* @return response status (400 or 500).
*/
public static Response.Status getResponseStatus(final ConstraintViolationException violation) {
final Iterator<ConstraintViolation<?>> iterator = violation.getConstraintViolations().iterator();
if (iterator.hasNext()) {
for (final Path.Node node : iterator.next().getPropertyPath()) {
final ElementKind kind = node.getKind();
if (ElementKind.RETURN_VALUE.equals(kind)) {
return Response.Status.INTERNAL_SERVER_ERROR;
}
}
}
return Response.Status.BAD_REQUEST;
}
示例8: printPropertyPath
import javax.validation.Path; //导入方法依赖的package包/类
private String printPropertyPath(Path path) {
if (path == null) return "UNKNOWN";
String propertyPath = "";
Path.Node parameterNode = null;
// Construct string representation of property path.
// This will strip away any other nodes added by RESTEasy (method, parameter, ...).
for (Path.Node node : path) {
if (node.getKind() == ElementKind.PARAMETER) {
parameterNode = node;
}
if (node.getKind() == ElementKind.PROPERTY) {
if (!propertyPath.isEmpty()) {
propertyPath += ".";
}
propertyPath += node;
}
}
if (propertyPath.isEmpty() && parameterNode != null) {
// No property path constructed, assume this is a validation failure on a request parameter.
propertyPath = parameterNode.toString();
}
return propertyPath;
}
示例9: isReachable
import javax.validation.Path; //导入方法依赖的package包/类
public boolean isReachable(Object traversableObject,
Path.Node traversableProperty,
Class<?> rootBeanType,
Path pathToTraversableObject,
ElementType elementType) {
//lazy, don't load
return Hibernate.isInitialized( traversableObject )
&& Hibernate.isPropertyInitialized( traversableObject, traversableProperty.getName() );
}
示例10: isCascadable
import javax.validation.Path; //导入方法依赖的package包/类
public boolean isCascadable(Object traversableObject,
Path.Node traversableProperty,
Class<?> rootBeanType,
Path pathToTraversableObject,
ElementType elementType) {
String path = getStringBasedPath( traversableProperty, pathToTraversableObject );
return ! associations.contains(path);
}
示例11: printPropertyPath
import javax.validation.Path; //导入方法依赖的package包/类
private String printPropertyPath(Path path) {
if (path == null) {
return "UNKNOWN";
}
String propertyPath = "";
Path.Node parameterNode = null;
// Construct string representation of property path.
// This will strip away any other nodes added by RESTEasy (method, parameter, ...).
for (Path.Node node : path) {
if (node.getKind() == ElementKind.PARAMETER) {
parameterNode = node;
}
if (node.getKind() == ElementKind.PROPERTY) {
if (!propertyPath.isEmpty()) {
propertyPath += ".";
}
propertyPath += node;
}
}
if (propertyPath.isEmpty() && parameterNode != null) {
// No property path constructed, assume this is a validation failure on a request parameter.
propertyPath = parameterNode.toString();
}
return propertyPath;
}
示例12: ValuePath
import javax.validation.Path; //导入方法依赖的package包/类
public ValuePath(Path validationPath) {
for (Path.Node node: validationPath) {
if (node.getIndex() != null)
elements.add(new PathSegment.Element(node.getIndex()));
if (node.getName() != null)
elements.add(new PathSegment.Property(node.getName()));
}
}
示例13: getConstraintViolationField
import javax.validation.Path; //导入方法依赖的package包/类
/**
* 通过根类和hibernate validator错误信息获取当前验证失败的field对象
*
* @param rootClass pojo根类, 如:User.class类包含属性info:Info.class,User即rootClass
* @param cv hibernate validator验证结果对象{@link ConstraintViolation}
* @return 当前失败的field对象
*/
static Field getConstraintViolationField(Class<?> rootClass, ConstraintViolation<Object> cv) {
try {
Field field = null;
for (Path.Node node : cv.getPropertyPath()) {
String fieldName = node.getName();
field = rootClass.getDeclaredField(fieldName);
rootClass = field.getType();
}
return field;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例14: toResponse
import javax.validation.Path; //导入方法依赖的package包/类
@Override
public Response toResponse(ConstraintViolationException exception) {
logger.info(String.format("%s. Returning %s response.", exception, Response.Status.BAD_REQUEST));
if (logger.isDebugEnabled()) {
logger.debug(StringUtils.EMPTY, exception);
}
// start with the overall message which will be something like "Cannot create xyz"
final StringBuilder errorMessage = new StringBuilder(exception.getMessage()).append(" - ");
boolean first = true;
for (final ConstraintViolation violation : exception.getConstraintViolations()) {
if (!first) {
errorMessage.append(", ");
}
first = false;
// lastNode should end up as the field that failed validation
Path.Node lastNode = null;
for (final Path.Node node : violation.getPropertyPath()) {
lastNode = node;
}
// append something like "xyz must not be..."
errorMessage.append(lastNode.getName()).append(" ").append(violation.getMessage());
}
return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage.toString()).type("text/plain").build();
}
示例15: readPropertyName
import javax.validation.Path; //导入方法依赖的package包/类
private static String readPropertyName(ConstraintViolation violation) {
Iterator<Path.Node> i = violation.getPropertyPath().iterator();
Path.Node current = i.next();
while (i.hasNext()) {
current = i.next();
}
// Use '.' as the key if the entire object is erroneous
if ("arg0".equals(current.getName())) {
return ".";
}
else {
return violation.getPropertyPath().toString();
}
}