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; } }
@Override public Response toResponse(MethodConstraintViolationException ex) { Map<String, String> errors = new HashMap<String, String>(); for (MethodConstraintViolation<?> methodConstraintViolation : ex.getConstraintViolations()) { errors.put( methodConstraintViolation.getParameterName(), methodConstraintViolation.getMessage()); } return Response.status(Status.PRECONDITION_FAILED).entity(errors).build(); }