【发布时间】:2015-03-02 10:01:16
【问题描述】:
所以我想更改用于通过 DropWizard 资源验证模型的验证消息。
我正在使用 java bean 验证注释。例如,这是我要验证的字段之一:
@NotEmpty(message = "Password must not be empty.")
我可以使用验证器按预期测试它的工作原理。
但是,当我使用 DropWizard 对资源进行验证时,它会为该消息添加一些额外的内容。我看到的是这个 - password Password must not be empty. (was null) 并且我在这里找到了执行此操作的代码 - https://github.com/dropwizard/dropwizard/blob/master/dropwizard-validation/src/main/java/io/dropwizard/validation/ConstraintViolations.java
具体是这个方法-
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());
}
}
有什么方法可以覆盖这种行为吗?我只是想显示我在注释中设置的消息...
【问题讨论】:
标签: java jax-rs dropwizard