Overriding DropWizard ConstraintViolation Message

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

+6
source share
3 answers

ConstraintViolationExceptionMapper is the one that uses this method. To undo it, you need to unregister it and register your ExceptionMapper .

Delete exception configurator

Dropwizard 0.8

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

 server: registerDefaultExceptionMappers: false 

Dropwizard 0.7.x

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

Create and add your own exception calculator

 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 handler to your application class.

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

Here is the software 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<Throwable>() {}); env.jersey().register(new JsonProcessingExceptionMapper()); env.jersey().register(new EarlyEofExceptionMapper()); } 

I think it is more reliable than the configuration file. And, as you can see, this also allows you to return all other display filters by default .

+6
source

@ValidationMethod should be useful here. is not it?

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; } 
0
source

Source: https://habr.com/ru/post/983209/


All Articles