即使我使用无效值填充模型,以下代码也不会产生任何无效值。
private static ClassValidator<User> userValidator = new ClassValidator<User>(User.class);
InvalidValue[] errors = userValidator.getInvalidValues(user);
List<String> err = new ArrayList<String>();
for (InvalidValue invalidValue : errors) {
System.out.println(invalidValue.getMessage());
}
在评论中添加用户实体类。
@Entity(name = "User")
@NamedQueries({
@NamedQuery(name = MWQueries.nqAllUsers, query = "from User"),
@NamedQuery(name = MWQueries.nqUserById, query = "from User where id = ?")
})
public class User implements Serializable {
private static final long serialVersionUID = -5013553611664469461L;
private Long id;
private String userName;
private String email;
private Role role;
private @XStreamOmitField String passWord;
public User() {
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column
@Length(min = 6, max = 20)
@NotBlank
@NotEmpty
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column
@Length(min = 6)
@NotBlank
@NotEmpty
@JsonIgnore
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@Column
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
@Column(unique = true)
@Email
@NotBlank
@NotEmpty
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
答案 0 :(得分:1)
我能给你的唯一提示就是尝试一下。如果此测试验证有效,那么问题就在于您使用验证器的方式。如果它不起作用,则问题出现在您的实体中
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
/** given: an invalid object */
Demo invalid = new Demo("Hallo Welt");
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
/** then there must not be constraint violation */
Set<ConstraintViolation<Demo>> result = validator.validate(invalid);
答案 1 :(得分:0)
在验证javax约束后调用应用程序级别的hibernate验证器代码验证了hibernate验证。
public Validity validate(T bean, Class<T> beanClass) {
List<String> err = new ArrayList<String>();
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
javax.validation.Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<T>> violations = validator.validate(bean);
for (ConstraintViolation<T> violation : violations) {
String propertyPath = violation.getPropertyPath().toString().toLowerCase();
String message = violation.getMessage();
String errMsg = WordUtils.capitalize(propertyPath) + " " + message;
Log.error(EntityValidator.class, "Invalid Value: '" + errMsg);
err.add(errMsg);
}
// Checking for hibernate validations
err.addAll(hValidate(bean, beanClass));
return new Validity(err.isEmpty(), err);
}
public List<String> hValidate(T bean, Class<T> beanClass) {
List<String> err = new ArrayList<String>();
ClassValidator<T> hValidator = new ClassValidator<T>(beanClass);
InvalidValue[] hErrs = hValidator.getInvalidValues(bean);
for (InvalidValue invalidValue : hErrs) {
err.add(invalidValue.getMessage());
Log.error(EntityValidator.class, "Invalid Value: '" + invalidValue.getMessage());
}
return err;
}