【问题标题】:Override DropWizard ConstraintViolation message覆盖 DropWizard ConstraintViolation 消息
【发布时间】: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


    【解决方案1】:

    这是 dropwizard 0.8 中的编程解决方案:

    public void run(final MyConfiguration config, final Environment env) {
        AbstractServerFactory sf = (AbstractServerFactory) config.getServerFactory();
        // disable all default exception mappers
        sf.setRegisterDefaultExceptionMappers(false);
        // register your own ConstraintViolationException mapper
        env.jersey().register(MyConstraintViolationExceptionMapper.class)
        // restore other default exception mappers
        env.jersey().register(new LoggingExceptionMapper<Throwable>() {});
        env.jersey().register(new JsonProcessingExceptionMapper());
        env.jersey().register(new EarlyEofExceptionMapper());
    } 
    

    我认为它比配置文件更可靠。如您所见,它还启用了所有其他default exception mappers

    【讨论】:

    • 这应该是公认的答案,因为当前删除的异常映射器比 OP 要求的要多。
    【解决方案2】:

    ConstraintViolationExceptionMapper 是使用该方法的人。为了覆盖它,你需要注销它并注册你自己的ExceptionMapper

    移除异常映射器

    Dropwizard 0.8

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

    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.
        }
    }
    

    并在您的应用程序类中添加您的异常映射器。

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

    【讨论】:

    • 我更喜欢Lukasz Wiktor's answer,因为他重新设置了这个答案的第一步删除的其他异常映射器,有效地改变了只有ConstraintValidationExceptions的映射器
    【解决方案3】:

    @ValidationMethod 在这里应该很有用。不是吗?

    http://www.dropwizard.io/0.9.0/docs/manual/validation.html

    @ValidationMethod(message="Password cannot be empty")
    @JsonIgnore
    public boolean isPasswordProvided() {
        return false if password not provided;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2016-09-13
      • 2020-04-16
      • 2019-07-18
      • 1970-01-01
      相关资源
      最近更新 更多