覆盖DropWizard ConstraintViolation消息 [英] Override DropWizard ConstraintViolation message

查看:88
本文介绍了覆盖DropWizard ConstraintViolation消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想更改用于通过DropWizard资源验证模型的验证消息.

So I want to change the validation messages used to validate a model through a DropWizard resource.

我正在使用Java bean验证批注.例如,这是我要验证的字段之一:

I'm using java bean validation annotations. For example here is one of the fields I want to validate:

@NotEmpty(message = "Password must not be empty.")

我可以使用验证器按预期测试此作品.

I can test this works as expected using a validator.

但是,当我使用DropWizard对资源进行验证时,它会向该消息添加一些额外的内容.我看到的是-password Password must not be empty. (was null),并且我在这里找到了执行此操作的代码-

However when I use DropWizard to do the validation on the resource it adds some extra stuff to that message. What I see is this - password Password must not be empty. (was null) and I've found the code that does this here - 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());
    }
}

有什么办法可以替代此行为?我只想显示我在注释中设置的消息...

Is there any way I can override this behaviour? I just want to display the message that I set in the annotation...

推荐答案

ExceptionMapper .

ConstraintViolationExceptionMapper is the one which uses that method. In order to override it, you need to deregister it and register your own ExceptionMapper.

删除异常映射器

Dropwizard 0.8

将以下内容添加到您的yaml文件中.请注意,它将删除dropwizard添加的所有默认异常映射器.

Add the following to your yaml file. Note that it will remove all the default exception mappers that dropwizard adds.

server:
    registerDefaultExceptionMappers: false

Dropwizard 0.7.x

environment.jersey().getResourceConfig().getSingletons().removeIf(singleton -> singleton instanceof ConstraintViolationExceptionMapper);

创建并添加自己的异常映射器

public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

    @Override
    public Response toResponse(ConstraintViolationException exception) {
        // get the violation errors and return the response you want.
    }
}

并将异常映射器添加到您的应用程序类中.

and add your exception mapper in your application class.

public void run(T configuration, Environment environment) throws Exception {
  environment.jersey().register(ConstraintViolationExceptionMapper.class);
}

这篇关于覆盖DropWizard ConstraintViolation消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆