本文整理汇总了Java中javax.validation.ValidatorFactory.getValidator方法的典型用法代码示例。如果您正苦于以下问题:Java ValidatorFactory.getValidator方法的具体用法?Java ValidatorFactory.getValidator怎么用?Java ValidatorFactory.getValidator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.validation.ValidatorFactory
的用法示例。
在下文中一共展示了ValidatorFactory.getValidator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loads
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Test
public void loads() {
Pessoa pessoa = new Pessoa();
pessoa.setNome("João da Silva");
pessoa.setCpf("12345678901");
pessoa.setTelefone("(11)3266-4455");
pessoa.setEndereco("Rua A, 123, Bananal, Guarulhos - SP");
Check check = new SafeguardCheck();
/*Validação manual usando a interface Check*/
Check resultados = check.elementOf(pessoa).validate();
int quantidadeDeElementosInvalidos = resultados.getInvalidElements().size();
boolean temErro = resultados.hasError();
/*Validação pelo provedor de validação, usando javax.validation*/
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
javax.validation.Validator validator = factory.getValidator();
Set<ConstraintViolation<Pessoa>> violacoes = validator.validate(pessoa, Default.class);
Assert.assertEquals(1, quantidadeDeElementosInvalidos);
Assert.assertEquals(true, temErro);
Assert.assertEquals(1, violacoes.size());
}
示例2: ValidatorProviderJsr303
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
public ValidatorProviderJsr303()
{
ValidatorFactory validatorFactory = null;
try {
validatorFactory = Validation.buildDefaultValidatorFactory();
if (validatorFactory.getValidator() == null) {
validatorFactory = null;
}
} catch (Throwable t) {
log.log(Level.WARNING, t.toString(), t);
}
_validatorFactory = validatorFactory;
}
示例3: testValidation
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Test
public void testValidation() throws Exception {
// 検証対象の値を格納したBean
InputBean bean = new InputBean();
bean.setCode("123456");
// Validatorの生成
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator v = factory.getValidator();
// 検証結果を取得、検証メッセージを表示
Set<ConstraintViolation<InputBean>> result = v.validate(bean);
for (ConstraintViolation<InputBean> cv : result) {
System.out.println(cv.getMessage());
System.out.println(cv.getConstraintDescriptor().getAnnotation());
System.out.println(cv.getPropertyPath());
}
}
示例4: shouldValidate
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Test
public void shouldValidate() {
Person person = Person.builder()
.km(34d)
.email("przodownikR1gmail.com")
.firstname(null)
.birthDay(ZonedDateTime.now())
.modify(LocalDate.now())
.build();
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Person>> validators = validator.validate(person);
Assertions.assertThat(validators).hasSize(2);
ConstraintViolation<Person> validation = validators.iterator().next();
for(ConstraintViolation<Person> constraint : validators){
//log.info("{}",constraint);
log.info("property name : {}, message : {} , messageTemplate : {}",constraint.getPropertyPath(),constraint.getMessage(),constraint.getMessageTemplate());
}
Assertions.assertThat(validators.stream().filter(p->p.getMessage().contains("not be null")).findFirst().isPresent());
}
示例5: JValidator
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@SuppressWarnings({ "unchecked", "rawtypes" })
public JValidator(URL url) {
this.clazz = ReflectUtils.forName(url.getServiceInterface());
String jvalidation = url.getParameter("jvalidation");
ValidatorFactory factory;
if (jvalidation != null && jvalidation.length() > 0) {
factory = Validation.byProvider((Class)ReflectUtils.forName(jvalidation)).configure().buildValidatorFactory();
} else {
factory = Validation.buildDefaultValidatorFactory();
}
this.validator = factory.getValidator();
}
示例6: EntityManagerImpl
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
public EntityManagerImpl(SqlSessionFactory sqlSessionFactory) {
super(sqlSessionFactory);
try {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
this.validator = factory.getValidator();
} catch (Exception e) {
logger.warn("Unable to create validator");
}
}
示例7: testDefaultMetadataBeanValidation
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Test
public void testDefaultMetadataBeanValidation() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<MzTab>> violations = validator.validate(
createTestFile());
for (ConstraintViolation<MzTab> violation : violations) {
System.err.println("Validation error at " + violation.
getPropertyPath().
toString() + ": " + violation.getMessage());
}
}
示例8: validateTodoInValid
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Test
public void validateTodoInValid() {
ToDo toDo = new ToDo("[email protected]","c", "description 1", 6);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<ToDo>> constraintViolations = validator.validate(toDo);
if (!constraintViolations.isEmpty()) {
Iterator itr = constraintViolations.iterator();
while (itr.hasNext()) {
ConstraintViolation next = (ConstraintViolation)itr.next();
Assert.assertEquals("size must be between 2 and 256", next.getMessage());
}
}
}
示例9: setUpValidator
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@BeforeClass
public static void setUpValidator() {
ValidatorFactory factory = Validation.byProvider( MinijaxValidationProvider.class )
.configure()
.buildValidatorFactory();
validator = factory.getValidator();
}
示例10: testValid
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Test
public void testValid() {
TextField text = new TextField();
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Binder<MyEntity> binder = new BeanValidationBinder<>(MyEntity.class);
binder.forField(text).withNullRepresentation("").bind("text");
MyEntity t = new MyEntity();
// valid
t.setText("bla");
binder.setBean(t);
assertTrue(validator.validate(t).isEmpty());
assertTrue(binder.validate().isOk());
text.setValue("");
assertNull(t.getText());
/*
* // invalid t.setText(""); binder.setBean(t);
* assertFalse(validator.validate(t).isEmpty());
* assertFalse(binder.validate().isOk());
*
* t.setText(null); binder.setBean(t);
* assertTrue(validator.validate(t).isEmpty());
* assertTrue(binder.validate().isOk());
*/
}
示例11: setup
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Before
public void setup() {
metadataService = mock(MetadataService.class);
flowPersistenceProvider = mock(FlowPersistenceProvider.class);
snapshotSerializer = mock(VersionedProcessGroupSerializer.class);
final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
validator = validatorFactory.getValidator();
registryService = new RegistryService(metadataService, flowPersistenceProvider, snapshotSerializer, validator);
}
示例12: validateBean
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
/**
* Valide un bean
*
* @param bean
* @throws CustomException
*/
public static <T> Boolean validateBean(T bean, Logger logger) {
logger.debug(" ***VALIDATION*** : " + bean);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> constraintViolations = validator.validate(bean);
if (constraintViolations != null && constraintViolations.size() > 0) {
for (ConstraintViolation<?> violation : constraintViolations) {
logger.debug(" *** " + violation.getPropertyPath().toString() + " : " + violation.getMessage());
}
return false;
}
return true;
}
示例13: validate
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Override
public List<ValidationMessage> validate(MzTab mzTab) {
List<ValidationMessage> list = new LinkedList<>();
ValidatorFactory validatorFactory = Validation.byDefaultProvider()
.configure()
.buildValidatorFactory();
javax.validation.Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<MzTab>> violations = validator.validate(mzTab);
for(ConstraintViolation<MzTab> violation:violations) {
list.add(new ValidationMessage().message(getPathLocatorString(
violation)+": "+violation.getMessage()).messageType(ValidationMessage.MessageTypeEnum.ERROR));
}
return list;
}
示例14: setUp
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
validator = validatorFactory.getValidator();
news = new News();
}
示例15: validate
import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
/**
* Validates the {@link TelegramBotMillResponse}.
*
* @param response
* the response
* @return true if the response is valid, false otherwise.
*/
protected boolean validate(TelegramBotMillResponse response) {
// If validations are not enabled, returns true.
if (!TelegramBotMillContext.getInstance().isValidationEnabled()) {
return true;
}
boolean valid = true;
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<TelegramBotMillResponse>> violations = validator
.validate(response);
for (ConstraintViolation<TelegramBotMillResponse> v : violations) {
valid = false;
logger.error(
"TelegramBotMillResponse validation error. Message: [{}] Value: [{}], Class: [{}], Field: [{}]",
v.getMessage(), v.getInvalidValue(), v.getRootBean(),
v.getPropertyPath());
}
if (valid == false) {
// Sends the constraint violations through the callback.
List<TelegramBotMillMonitor> registeredMonitors = TelegramBotMillContext
.getInstance().getRegisteredMonitors();
for (TelegramBotMillMonitor monitor : registeredMonitors) {
// TODO
// monitor.onValidationError(response, violations);
}
}
return valid;
}