0
代碼在這裏我想用Bean驗證:如何以編程方式配置bean驗證?
@Inject
private ValidatorFactory validatorFactory;
....
public Response create(@Context HttpServletRequest request) {
...
Set<ConstraintViolation<UserDTO>> validate = validatorFactory.getValidator().validate(userDTO);
validate.forEach(error-> System.err.println(error.getMessage()));
if(validate.size() > 0){
throw new ValidationException("userDTO is not valid!");
}
...
}
public Response update(@Context HttpServletRequest request) {
Set<ConstraintViolation<UserDTO>> validate = validatorFactory.getValidator().validate(userDTO);
validate.forEach(error-> System.err.println(error.getMessage()));
if(validate.size() > 0){
throw new ValidationException("userDTO is not valid!");
}
...
}
UserDTO:
public class UserDTO {
private Integer id;
private String userName;
@NotNull(message = "is missing")
private String locked;
@Email(message = "email is not valid")
private String email;
@NotNull(message = "countryCode is missing")
private String countryCode;
...getters-setters more variables...
}
所以我要在這裏我想創建一個用戶一個POST HTTP方法...豆驗證工作,如果丟失國家代碼或鎖定好或如果電子郵件無效正常...但是,當我想只是更新例如電子郵件驗證也運行在整個dto類...所以問題是我可以和我如何配置不運行對每一個變量每次?
謝謝,這就是我想要的:) – kodaek98