2012-05-07 87 views
0

不確定這是否可能。想知道如果我可以爲bean的字段創建一個自定義約束,它將使用同一個類對象的其他字段的值。使用其他字段值的bean字段的自定義驗證器

public final class AWRRecordObject extends DoaAbstract implements 
     java.io.Serializable { 
    private static final long serialVersionUID = 1L; 

    @ValidateApplicationType 
    private String applicationType; 

    private String recordData; 

    private String programSource; 

    // ... 
} 

public class ApplicationTypeValidator implements 
     ConstraintValidator<ValidateApplicationType, AWRRecordObject> { 

    private Class<AWRRecordObject> awrRecObj; // ? 

    @Override 
    public void initialize(ValidateApplicationType constraintAnnotation) { 
     this.awrRecObj = constraintAnnotation.value(); 
    } 

    @Override 
    public boolean isValid(String arg0, ConstraintValidatorContext arg1) { 

     // verify if the application type is A1 and length is 512 
     if ((awrRecObj.getApplicationType() 
       .equalsIgnoreCase(AWRConstant.AWR_W2)) 
       && (awrRecObj.getRecordData().toString().length() != AWRConstant.W2_RECORD_LEN) 
       && (awrRecObj.getProgramSource() 
         .equalsIgnoreCase(AWRConstant.SOURCE_BATCH))) { 

      // create AWRRequestError Object and add it to AWRRecordObject 
      AWRUtility.createAWRRequestErrorObject(awrRecObj, 
        AWRErrorTypeCode.W2_RECORD_LENGTH_ERROR, 
        awrRecObj.getRecordData()); 
      return false; 
      // ... 
     } 

     return true; 
    } 
} 

@Target({ METHOD, FIELD, ANNOTATION_TYPE }) 
@Retention(RUNTIME) 
@Documented 
@Constraint(validatedBy = { ApplicationTypeValidator.class }) 
public @interface ValidateApplicationType { 

    String message() default "{com.myproject.validation.ApplicationType.message}"; 

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

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

    Class<AWRRecordObject> value(); 
} 

的代碼讀取一個固定長度的文件,並將解析值到一個bean:

public static AWRRecordObject bufferedReader() throws IOException { 

    int lineCount = 0; 
    long time1 = System.currentTimeMillis(); 
    File file = new File("D://TestData/test.txt"); 
    BufferedReader br = new BufferedReader(new FileReader(file), 8192); 
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); 
    validator = factory.getValidator(); 
    String line; 

    while ((line = br.readLine()) != null) { 

     String type = line.length() > 2 ? line.substring(0, 2) : line; 

     AWRRecordObject record = new AWRRecordObject(line, type, lineCount); 

     Set<ConstraintViolation<AWRRecordObject>> constraintViolations = validator 
       .validate(record); 

     if (constraintViolations.size() > 0) { 

      Iterator it = constraintViolations.iterator(); 
      while (it.hasNext()) { 
       ConstraintViolation<AWRRecordObject> cv = (ConstraintViolation<AWRRecordObject>) it 
         .next(); 
       System.out.println(cv.getInvalidValue()); 
       System.out.println(cv.getMessage()); 

      } 

     } 

     return record; 
    } 
} 

不幸的是,我收到以下錯誤消息:

我試着用下面的代碼實現這
Exception in thread "main" javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.String 
at org.hibernate.validator.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:383) 

回答

0

看起來像有你需要做兩件事情:

1。您目前已將自定義驗證應用於目標bean的字段。您需要將其移至課程中。

@ValidateApplicationType 
public final class AWRRecordObject 

2。您的註釋定義需要進行更改,以便將註釋應用於類。

@Target({ TYPE, ANNOTATION_TYPE }) 
+0

我會試試看。與此同時,我試圖在具有三個字段作爲參數的類的構造函數上進行自定義驗證。 –

相關問題