1

I want to create my own custom validation error message. I searched so many forums but I am not sure where I am going wrong.

@Provider
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ValidationException>{
private String field;
private String code;
private String message;

public ConstraintViolationExceptionMapper(){
}

@Override
public Response toResponse(ValidationException e) {
    message = e.getMessage();
    InvalidRespose invalidRespose = new InvalidRespose(code, message, field);
    return Response.status(400).entity(invalidRespose).type(MediaType.APPLICATION_JSON).build();
}
}

Resource class

@POST
@Path("/increment")
@Consumes(MediaType.APPLICATION_JSON)
@Timed
public Response greetings(@Valid @NotNull @Pattern(regexp = "^[1-9]\\d*$", message = "Should be a positive Number") @PathParam("id") String account_id){

    ...
    ...
}

I have registered my ExceptionMapper in my Applicationclass as

environment.jersey().register(new ConstraintViolationExceptionMapper());

I am not sure where I am going wrong.

Vicky
  • 151
  • 1
  • 10

1 Answers1

1

I had to disable Dropwizard's default Mappers before registering my own.

((DefaultServerFactory)rateLimitConfiguration.getServerFactory()).setRegisterDefaultExceptionMappers(false);

Reference Link here

Community
  • 1
  • 1
Vicky
  • 151
  • 1
  • 10