【问题标题】:MyCustomExceptionMapper Not invoked - Custom validation error Message - Jersey ValidationMyCustomExceptionMapper 未调用 - 自定义验证错误消息 - Jersey Validation
【发布时间】:2016-09-21 14:09:36
【问题描述】:

我想创建自己的自定义验证错误消息。我搜索了很多论坛,但我不确定我哪里出错了。

@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();
}
}

资源类

@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){

    ...
    ...
}

我已将我的 ExceptionMapper 在我的 Applicationclass 中注册为

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

我不确定我哪里出错了。

【问题讨论】:

    标签: java validation jackson jersey-2.0 dropwizard


    【解决方案1】:

    在注册我自己的之前,我必须禁用 Dropwizard 的默认映射器。

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

    参考链接here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 2018-02-08
      • 2016-04-18
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多