So, I want to change the validation messages used to validate the model using the DropWizard resource.
I am using java bean validation annotations. For example, here is one of the fields that I want to check:
@NotEmpty(message = "Password must not be empty.")
I can check how this works using the validator.
However, when I use DropWizard to perform a check on a resource, it adds additional content to this message. I see this - password Password must not be empty. (was null) password Password must not be empty. (was null) and I found the code that does this here - https://github.com/dropwizard/dropwizard/blob/master/dropwizard-validation/src/main/java/io/dropwizard/validation/ConstraintViolations.java
In particular, this method is
public static <T> String format(ConstraintViolation<T> v) { if (v.getConstraintDescriptor().getAnnotation() instanceof ValidationMethod) { final ImmutableList<Path.Node> nodes = ImmutableList.copyOf(v.getPropertyPath()); final ImmutableList<Path.Node> usefulNodes = nodes.subList(0, nodes.size() - 1); final String msg = v.getMessage().startsWith(".") ? "%s%s" : "%s %s"; return String.format(msg, Joiner.on('.').join(usefulNodes), v.getMessage()).trim(); } else { return String.format("%s %s (was %s)", v.getPropertyPath(), v.getMessage(), v.getInvalidValue()); } }
Is there any way to override this behavior? I just want to display the message that I set in the annotation ...
source share