public Type getConstraintType(Object o) { if (!(o instanceof ConstraintViolation)) { throw new RuntimeException(Messages.MESSAGES.unknownObjectPassedAsConstraintViolation(o)); } ConstraintViolation<?> v = ConstraintViolation.class.cast(o); if (v instanceof MethodConstraintViolation) { MethodConstraintViolation<?> mv = MethodConstraintViolation.class.cast(v); return mv.getKind() == MethodConstraintViolation.Kind.PARAMETER ? Type.PARAMETER : Type.RETURN_VALUE; } Object b = v.getRootBean(); String fieldName = null; Iterator<Node> it = v.getPropertyPath().iterator(); while (it.hasNext()) { Node node = it.next(); fieldName = node.getName(); if (fieldName == null) { return Type.CLASS; } } String getterName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); try { // getMethod(v.getLeafBean().getClass(), getterName); getMethod(v.getRootBeanClass(), getterName); return Type.PROPERTY; } catch (NoSuchMethodException e) { return Type.FIELD; } }
@SuppressWarnings("rawtypes") @Override public Response toResponse(javax.validation.ValidationException exception) { Response.Status errorStatus = Response.Status.INTERNAL_SERVER_ERROR; List<String> errorsList = new ArrayList<String>(); if (exception instanceof ConstraintViolationException) { ConstraintViolationException constraint = (ConstraintViolationException) exception; Iterator i$ = constraint.getConstraintViolations().iterator(); while (i$.hasNext()) { ConstraintViolation violation = (ConstraintViolation) i$.next(); String errorMessage = this.getPropertyName(violation) + ": " + violation.getMessage(); errorsList.add(errorMessage); LOG.error( violation.getRootBeanClass().getSimpleName() + "." + violation.getPropertyPath() + ": " + violation.getMessage()); } if (!(constraint instanceof ResponseConstraintViolationException)) { errorStatus = Response.Status.BAD_REQUEST; } } String errorsAsString = StringUtils.join(errorsList, ", "); Response response = Response.status(errorStatus).entity(errorsAsString).build(); return response; }
public void testBeanValidation() throws Exception { Artist a = new Artist(); a.setName("TOO OLD ARTIST"); a.setAge(120); XPersistence.getManager().persist(a); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof javax.validation.ConstraintViolationException) { javax.validation.ConstraintViolationException vex = (javax.validation.ConstraintViolationException) ex.getCause(); assertEquals("1 invalid value is expected", 1, vex.getConstraintViolations().size()); ConstraintViolation violation = vex.getConstraintViolations().iterator().next(); assertEquals("Bean", "Artist", violation.getRootBeanClass().getSimpleName()); String expectedMessage = "es".equals(Locale.getDefault().getLanguage()) ? "tiene que ser menor o igual que 90" : "must be less than or equal to 90"; assertEquals("Message text", expectedMessage, violation.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
private void displayContraintViolations(Set<ConstraintViolation<Book03>> constraintViolations) { for (ConstraintViolation constraintViolation : constraintViolations) { System.out.println( "### " + constraintViolation.getRootBeanClass().getSimpleName() + "." + constraintViolation.getPropertyPath() + " - Invalid Value = " + constraintViolation.getInvalidValue() + " - Error Msg = " + constraintViolation.getMessage()); } }
/* * (non-Javadoc) * * @see java.lang.Throwable#getMessage() */ @Override public String getMessage() { String message = "There are violations [" + ls; for (ConstraintViolation<AbstractValidatedCatalogue> violation : violations) { message += violation.getRootBeanClass().getName() + "." + violation.getPropertyPath() + " " + violation.getMessage() + ls; } message += "]"; return message; }