Hibernate Validator異常“找不到類型為java.lang.String的驗證器”
您將放置@ActorConstraint到name類型為的字段中String,而驗證器類型使用設置了ActorConstraintValidator參數Actor。也就是說,沒有@ActorConstraint約束和類型的驗證器String。
解決方法我使用Spring和Spring MVC 3.1,Hibernate 3,Hibernate Validator 4.3。
我想添加我的自定義約束驗證器,但似乎未調用我的驗證器,并且總是收到“找不到類型的驗證器”異常。我有沒有犯錯?我總是遇到這個異常的大問題
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.String. at org.hibernate.validator.internal.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:394) at org.hibernate.validator.internal.engine.ConstraintTree.findMatchingValidatorClass(ConstraintTree.java:375)
但是,如果我刪除了自定義約束驗證器,一切都會很好。這是我的代碼,您能幫忙找到導致此異常的原因嗎?
test.constraint.ActorConstraint@Target({ElementType.METHOD,ElementType.FIELD,ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Constraint(validatedBy=ActorConstraintValidator.class)public @interface ActorConstraint { String message() default 'Actor already exists in database'; public abstract Class<?>[] groups() default {}; public abstract Class<? extends Payload>[] payload() default {};}test.constraint.ActorConstraintValidator@Componentpublic class ActorConstraintValidator implements ConstraintValidator<ActorConstraint,Actor> { @Autowired private ActorModel actorModel; @Override public void initialize(ActorConstraint arg0) { System.out.println('ActorConstraintValidator is initialized'); } @Override public boolean isValid(Actor actor,ConstraintValidatorContext context) { boolean pass = true; if (getActorModel().isActorExist(actor.getName()) ){ pass = false; } return pass; } public ActorModel getActorModel() { return actorModel; } public void setActorModel(ActorModel actorModel) { this.actorModel = actorModel; } }test.entity.Actor@Entity /*(findByName named query definition is omitted)*/public class Actor implements Serializable { public static final String NAMED_QUERY__FIND_ACTOR_BY_NAME = 'findActorByName'; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ActorConstraint @NotEmpty private String name; @DateTimeFormat(iso=ISO.DATE) private Date birthday; @Min(0) @Max(300) private Integer height; ... }
特別說明,我已添加
<bean />
彈簧xml
相關文章:
1. thinkPHP5中獲取數據庫數據后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙2. python小白 關于類里面的方法獲取變量失敗的問題3. Python2中code.co_kwonlyargcount的等效寫法4. python小白,關于函數問題5. 求救一下,用新版的phpstudy,數據庫過段時間會消失是什么情況?6. javascript - 如何用最快的速度C#或Python開發一個桌面應用程序來訪問我的網站?7. mysql數據庫做關聯一般用id還是用戶名8. linux運維 - python遠程控制windows如何實現9. python - 如何對列表中的列表進行頻率統計?10. django - Python error: [Errno 99] Cannot assign requested address
