bean:
public class Dept {
@NotNull
private String deptid;
public String getDeptid() {
return deptid;
}
public void setDeptid(String deptid) {
this.deptid = deptid;
}
}
service代码:
@Service
public class DeptService {
@Resource
private Validator validator;
public void validate() {
Dept dept = new Dept();
Set<ConstraintViolation<Dept>> set = validator.validate(dept);
for (ConstraintViolation<Dept> record : set) {
// deptid
record.getPropertyPath();
// 不能为null
record.getMessage();
}
}
}