Override DropWizard ConstraintViolation message

前端 未结 3 381
终归单人心
终归单人心 2020-12-11 05:35

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

I\'m using java bean validation annotations. For example here is

3条回答
  •  佛祖请我去吃肉
    2020-12-11 06:25

    Here is a programmatic solution in 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() {});
        env.jersey().register(new JsonProcessingExceptionMapper());
        env.jersey().register(new EarlyEofExceptionMapper());
    } 
    

    I think it's more reliable than a config file. And as you can see it also enables back all other default exception mappers.

提交回复
热议问题