寻找一种向bean验证异常添加数据的方法

时间:2013-08-12 18:56:18

标签: java bean-validation

我有一个自定义bean验证器,它实现boolean isValid(Object, ConstraintValidatorContext)执行一些业务逻辑并根据该逻辑返回true / false。

我希望能够做的是将一些数据添加到抛出的ConstraintViolation异常中。捕获并处理它的任何异常处理程序都可以梳理出这些额外的数据,其中包含有关导致它的错误/ arg的更多详细信息。现在我只能将消息与缺少这些动态细节的违规相关联。

例如,传入isValid的对象包含一个映射,其内容由验证器验证。因为验证器API只返回一个布尔值,所以我失去了映射的哪些成员触发约束违规的粒度。我正在寻找一些能够保存并传递这些信息的方法。

EDIT 8/13/2013 - 涉及JAX-RS的示例解决方案

(遗漏了一些与此问题无关的JAXB详细信息)

FooParam.java

public class FooParam {
  private Map<String, String> subParamMap;

  public Map<String, String> getSubParamMap() { return this.subParamMap; }
  public void setSubParamMap(Map<String, String> subParamMap) {
    this.subParamMap = subParamMap;
  }

  public FooParam() { this.subParamMap = new HashMap<>(); }
}

FooResource.java

public class FooResource {

  ...

  public void doSomething(@CheckValidParam(value=FooResoure.class) FooParam fooParam) { 
    ...
  }
}

CheckValidParam.java

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = CheckValidParamValidator.class)
@Documented
public @interface CheckValidParam{

    String message() default "{com.foo.bar.CheckValidParam.message}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default  { };

    Class<?> value();

    @Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @interface List {
        CheckValidParam[] value();
    }
}

CheckValidParamValidator.java

public class CheckValidParamValidator implements ConstraintValidator<CheckValidParam, FooParam> {

    private Class<?> entityClass;

    @Override
    public void initialize(CheckValidParam checkValidParam ) {
        this.entityClass = checkValidParam.value();
    }

    @Override
    public boolean isValid(FooParam fooParam, ConstraintValidatorContext constraintValidatorContext) {

        String message = constraintValidatorContext.getDefaultConstraintMessageTemplate();
        constraintValidatorContext.disableDefaultConstraintViolation();
        boolean isValid = true;
        Map<String, String> subParamMap = fooParam.getSubParamMap();

        for (Map.Entry<String, String> entry : subParamMap.entrySet()) {
          //Contrived validation logic
          if (entry.getValue().equalsIgnoreCase("junk")) {   
            constraintValidatorContext.buildConstraintViolationWithTemplate(message)
                                      .addNode(entry.getKey()).addConstraintViolation();
            isValid = false;
           }
        }
        return isValid;
    }
}

剥离了类的版本,它拦截了JAX-RS管道中抛出的MethodConstraintViolationExceptions,以说明撤出无效的params和相关的错误消息。

MethodConstraintValidationMapper.java

@Provider
public class MethodConstraintValidationMapperimplements ExceptionMapper<MethodConstraintViolationException> {

    private static final Logger log = Logger.getLogger(MethodConstraintValidationMapper.class);

    @Override
    public Response toResponse(MethodConstraintViolationException ex) {
        Response response;
        Set<MethodConstraintViolation<?>> violations = ex.getConstraintViolations();
        List<ValidationExceptionError> errors = new ArrayList<>();
        for (MethodConstraintViolation<?> methodConstraintViolation : ex.getConstraintViolations()) {
            ValidationExceptionError error = new ValidationExceptionError();
            error.setFieldName(((PathImpl) methodConstraintViolation.getPropertyPath()).getLeafNode().asString());
            error.setErrorMessage(methodConstraintViolation.getMessage());
            errors.add(error);
        }
        return Response.status(Response.Status.PRECONDITION_FAILED).entity(new GenericEntity<List<ValidationExceptionError>>(errors) {}).type(MediaType.APPLICATION_JSON).build();
    }
}

1 个答案:

答案 0 :(得分:2)

您可以从ConstraintViolation对象中检索有关无效值的详细信息。特别是getPropertyPath()应该对您有所帮助,因为它允许导航到叶节点的路径,并通过Node#getKey()提供对相关键的访问(如果地图中的值已经过验证)。 / p>