未调用 MyCustomExceptionMapper - 自定义验证错误消息 - 泽西岛验证
MyCustomExceptionMapper Not invoked - Custom validation error Message - Jersey Validation
我想创建自己的自定义验证错误消息。我搜索了很多论坛,但我不确定我哪里出错了。
@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();
}
}
资源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){
...
...
}
我已经在我的应用程序中注册了我的 ExceptionMapperclass 为
environment.jersey().register(new ConstraintViolationExceptionMapper());
我不确定我哪里错了。
我必须在注册自己的之前禁用 Dropwizard 的默认映射器。
((DefaultServerFactory)rateLimitConfiguration.getServerFactory()).setRegisterDefaultExceptionMappers(false);
参考Link
我想创建自己的自定义验证错误消息。我搜索了很多论坛,但我不确定我哪里出错了。
@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();
}
}
资源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){
...
...
}
我已经在我的应用程序中注册了我的 ExceptionMapperclass 为
environment.jersey().register(new ConstraintViolationExceptionMapper());
我不确定我哪里错了。
我必须在注册自己的之前禁用 Dropwizard 的默认映射器。
((DefaultServerFactory)rateLimitConfiguration.getServerFactory()).setRegisterDefaultExceptionMappers(false);
参考Link