当前位置: 首页>>代码示例>>Java>>正文


Java ValidatorFactory.getValidator方法代码示例

本文整理汇总了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());
			  
}
 
开发者ID:gilmardeveloper,项目名称:java-validator-safeguard,代码行数:27,代码来源:TesteDeObjetosAnotados.java

示例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;
}
 
开发者ID:baratine,项目名称:baratine,代码行数:17,代码来源:ValidatorProviderJsr303.java

示例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());
    }
}
 
开发者ID:otsecbsol,项目名称:linkbinder,代码行数:19,代码来源:BeanValidatorTest.java

示例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());
 }
 
开发者ID:przodownikR1,项目名称:springJpaKata,代码行数:24,代码来源:ValidationPersonTest.java

示例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();
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:13,代码来源:JValidator.java

示例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");
	}
}
 
开发者ID:yaoakeji,项目名称:hibatis,代码行数:10,代码来源:EntityManagerImpl.java

示例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());
    }
}
 
开发者ID:nilshoffmann,项目名称:jmzTab-m,代码行数:13,代码来源:MzTabValidatorTest.java

示例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());
        }
    }
}
 
开发者ID:apssouza22,项目名称:java-microservice,代码行数:16,代码来源:TodoIT.java

示例9: setUpValidator

import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@BeforeClass
public static void setUpValidator() {
	ValidatorFactory factory = Validation.byProvider( MinijaxValidationProvider.class )
			.configure()
			.buildValidatorFactory();
	validator = factory.getValidator();
}
 
开发者ID:minijax,项目名称:minijax,代码行数:8,代码来源:CarTest.java

示例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());
	 */
}
 
开发者ID:ljessendk,项目名称:easybinder,代码行数:33,代码来源:BeanValidationBinderStringTest.java

示例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);
}
 
开发者ID:apache,项目名称:nifi-registry,代码行数:12,代码来源:TestRegistryService.java

示例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;
}
 
开发者ID:EsupPortail,项目名称:esup-ecandidat,代码行数:20,代码来源:MethodUtils.java

示例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;
}
 
开发者ID:nilshoffmann,项目名称:jmzTab-m,代码行数:15,代码来源:MzTabValidator.java

示例14: setUp

import javax.validation.ValidatorFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
    ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
    validator = validatorFactory.getValidator();

    news = new News();
}
 
开发者ID:arcuri82,项目名称:pg6100,代码行数:8,代码来源:NewsTest.java

示例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;
}
 
开发者ID:BotMill,项目名称:telegram-botmill,代码行数:39,代码来源:TelegramBotMillBean.java


注:本文中的javax.validation.ValidatorFactory.getValidator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。