春季启动时自定义jpa验证

Dav*_*man 4 java validation spring bean-validation spring-boot

我有一个像这样的实体:

@Entity
@Table(name = "transaction_receiver")
public class TransactionReceiver implements Serializable, Addressable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @Column(name = "contact_id", nullable = false)
    private String contactId;

    @Column(name = "datev_number")
    private String datevNumber;

    @NotNull
    @Column(name = "receiver", nullable = false)
    private String receiver;

    @NotNull
    @Size(min = 22, max = 34)
    @Column(name = "iban", length = 34, nullable = false)
    private String iban;

    @Size(min = 8, max = 11)
    @Column(name = "bic", length = 11, nullable = false)
    private String bic;

    @NotNull
    @Column(name = "institute")
    private String institute;

    @Column(name = "company")
    private String company;
Run Code Online (Sandbox Code Playgroud)

和任务,做一个自定义验证"你可以提供一个空的iban,bic和研究所,没关系.但如果任何一个字段不为空,上面的constrait必须持有"

我正在寻找最优雅的方式来实现这一目标.

我目前的解决方案是 - 我认为有点脏,但工作 - 使用@PrePersist语句并从那里抛出异常

    @PrePersist
    public void checkBankData() {
        boolean ibanEmpty = iban == null || iban.isEmpty();
        boolean ibanValid = !ibanEmpty && iban.length() >= 22 && iban.length() <= 34;
        boolean bicEmpty = bic == null || bic.isEmpty();
        boolean bicValid = !bicEmpty && bic.length() >= 8 && bic.length() <= 11;
        boolean instituteEmpty = institute == null || institute.isEmpty();

        boolean validState = (ibanEmpty && bicEmpty && instituteEmpty) || ibanValid && bicValid;

        if (!validState) {
            throw new IllegalStateException(
                String.format(
                    "bank data is not empty and %s%s%s%s%s",
                    !ibanValid ? "iban has to be from 22 to 34 chars long" : "",
                    !ibanValid && !bicValid ? "and" : "",
                    !bicValid ? "bic has to be from 8 to 11 chars long" : "",
                    !ibanValid && !bicValid && instituteEmpty ? "and" : "",
                    instituteEmpty ? "institue must not be empty" : ""
                )
            );
        }
    }
Run Code Online (Sandbox Code Playgroud)

这对@Valid注释是不明智的.另一种方法是定义如下所述的自定义验证器:http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-customconstraints.html

但这对我的约束看起来真的太过分了.

没有其他优雅的方式吗?

谢谢

Ric*_*ila 5

使用Hibernate Validation API并不像看起来那么复杂,并且对于你的约束来说是一个很好的解决方案.然而,你可以得到一个简单的方法来定义使用Hibernate验证,因为我们在一个项目中都添加完几节课约束.您的约束将如下所示:

@Validate(method = "checkBankData", message = "{BankData.invalid.message}")
@Entity
@Table(name = "transaction_receiver")
public class TransactionReceiver implements Serializable, Addressable {
Run Code Online (Sandbox Code Playgroud)

为此,您需要定义@Validate注释和CustomValidator类.

@Target({ ElementType.TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = CustomValidator.class)
@Documented
/**
 * Annotation to allow custom validation against model classes
 */
public @interface Validate {

  /**
   * Validation message
   */
  String message();

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

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

  /**
   * Validation method name
   */
  String method() default "";
}


public class CustomValidator implements ConstraintValidator<Validate, BusinessObject> {

  private static Log log = LogFactory.getLog(CustomValidator.class);
  private String validator;


  @Override
  public void initialize(Validate constraintAnnotation) {
    validator = constraintAnnotation.method();
  }

  @Override
  public boolean isValid(BusinessObject bo, ConstraintValidatorContext constraintContext) {
    try {
      return isValidForMethod(bo);
    } catch (Exception e) {
      /* Error durante la ejecución de la condición o del validador */
      log.error("Error validating "+bo, e);
      return false;
    }
  }


  private boolean isValidForMethod(BusinessObject bo) throws Exception {
    Method validatorMethod =  ReflectionUtils.findMethod(bo.getClass(), validator, new Class[] {});
    if (validatorMethod != null) {
      /* Validator call */
      Boolean valid = (Boolean) validatorMethod.invoke(bo);
      return valid != null && valid;
    } else {
      /* Method not found */
      log.error("Validator method not found.");
      return false;
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

如果您计划定义更多约束,这种方法会很好.您可以使用更多功能扩展它,例如验证条件或添加多个验证等.

偏离主题:

  • 验证与Spring Boot无关,因此无需在您的问题中提及它.

  • serialVersionUID = 1L; 这是一个非常糟糕的主意.使用IDE serialVersionUID生成器为此字段填充1L不同的值.