本文整理汇总了Java中javax.validation.Validator.validate方法的典型用法代码示例。如果您正苦于以下问题:Java Validator.validate方法的具体用法?Java Validator.validate怎么用?Java Validator.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.validation.Validator
的用法示例。
在下文中一共展示了Validator.validate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import javax.validation.Validator; //导入方法依赖的package包/类
public void create(T entity) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity);
if(constraintViolations.size() > 0){
Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
while(iterator.hasNext()){
ConstraintViolation<T> cv = iterator.next();
System.err.println(cv.getRootBeanClass().getName()+"."+cv.getPropertyPath() + " " +cv.getMessage());
System.err.println(cv.getRootBeanClass().getSimpleName()+"."+cv.getPropertyPath() + " " +cv.getMessage());
}
}else{
getEntityManager().persist(entity);
}
}
示例2: validate
import javax.validation.Validator; //导入方法依赖的package包/类
@Override
public void validate(Object entity) throws HttpException {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Object>> violations = validator.validate(entity);
if (!violations.isEmpty()) {
List<ConstraintViolationDescription> errors = new ArrayList<>();
ConstraintViolationResponseError error = new ConstraintViolationResponseError();
error.setMessage(UNPROCESSABLE_ENTITY_MESSAGE);
for (ConstraintViolation<Object> violation : violations) {
String attribute = violation.getPropertyPath().toString();
String message = violation.getMessage();
errors.add(new ConstraintViolationDescription(message, attribute));
}
error.setErrors(errors);
throw new UnprocessableEntityException(error);
}
}
示例3: main
import javax.validation.Validator; //导入方法依赖的package包/类
public static void main(String[] args) {
String src = "src.org/hack/controller";
src = src.replaceAll("/", File.separator + File.separator);
System.out.println(src);
System.out.println("src\\org\\hack\\controller");
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
System.out.println(factory.getMessageInterpolator().getClass());
Validator validator = factory.getValidator();
Blog blog = new Blog("1", "2121", "212", new Date(), 2);
Set<ConstraintViolation<Blog>> constraintViolations = validator.validate(blog);
System.out.println(validator.getClass());
for (ConstraintViolation<Blog> c : constraintViolations) {
System.out.println(c.getInvalidValue() + ":" + c.getMessage());
}
}
示例4: main
import javax.validation.Validator; //导入方法依赖的package包/类
public static void main(String[] args) {
Email email = new Email();
email.setFrom("[email protected]");
email.setTo("someone");
email.setSubject("");
email.setBody(null);
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<Email>> violations = validator.validate(email);
for (ConstraintViolation<Email> constraintViolation : violations) {
String propertyPath = constraintViolation.getPropertyPath().toString();
String message = constraintViolation.getMessage();
System.out.println("invalid value for: '" + propertyPath + "': " + message);
}
}
示例5: validateConfiguration
import javax.validation.Validator; //导入方法依赖的package包/类
/**
* Validates the provided {@link StreamingAppConfiguration} against constraints provided to fields holding the
* configuration
* @param configuration
* @param outputStream
* @return
* @throws IOException
*/
protected boolean validateConfiguration(final StreamingAppConfiguration configuration, final OutputStream outputStream) throws IOException {
if(outputStream == null)
throw new IOException("Missing required output stream\n");
if(configuration == null) {
outputStream.write(MSG_MISSING_CFG_INSTANCE);
return false;
}
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<StreamingAppConfiguration>> violations = validator.validate(configuration);
if(violations.isEmpty())
return true;
for(ConstraintViolation<StreamingAppConfiguration> v : violations) {
StringBuffer error = new StringBuffer("Invalid configuration at: ");
error.append(v.getPropertyPath()).append(", error: ").append(v.getMessage()).append("\n");
outputStream.write(error.toString().getBytes());
outputStream.flush();
}
return false;
}
示例6: parsePolicyEnable
import javax.validation.Validator; //导入方法依赖的package包/类
public static JsonNode parsePolicyEnable(String jsonString, HttpServletRequest httpServletRequest) throws JsonParseException, JsonMappingException, IOException{
List<String> violation_message = new ArrayList<String>();
ObjectNode result = new_mapper.createObjectNode();
result.put("valid", false);
PolicyEnbale policyEnable = new_mapper.readValue(jsonString, PolicyEnbale.class);
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
Locale locale = LocaleUtil.getLocale(httpServletRequest);
MessageInterpolator interpolator = new LocaleSpecificMessageInterpolator(vf.getMessageInterpolator(), locale);
Validator validator = vf.usingContext().messageInterpolator(interpolator).getValidator();
Set<ConstraintViolation<PolicyEnbale>> set = validator.validate(policyEnable);
if (set.size() > 0 ){
for (ConstraintViolation<PolicyEnbale> constraintViolation : set) {
violation_message.add(constraintViolation.getMessage());
}
result.set("violation_message", new_mapper.valueToTree(violation_message));
return result;
}
//additional data manipulation
String new_json = policyEnable.transformInput();
result.put("valid", true);
result.put("new_json", new_json);
return result;
}
示例7: shouldNotValidateWhenFirstNameEmpty
import javax.validation.Validator; //导入方法依赖的package包/类
@Test
public void shouldNotValidateWhenFirstNameEmpty() {
LocaleContextHolder.setLocale(Locale.ENGLISH);
Person person = new Person();
person.setFirstName("");
person.setLastName("smith");
Validator validator = createValidator();
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person);
assertThat(constraintViolations.size()).isEqualTo(1);
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
assertThat(violation.getMessage()).isEqualTo("may not be empty");
}
示例8: doValidation
import javax.validation.Validator; //导入方法依赖的package包/类
/**
* Validoi annettu bean:in siinä annettuja BeanValidation annotaatio-ehtoja vasten.
*
* @param bean
* Validoitava bean.
* @return Listaus löytyneistä validointivirheistä.
*/
public Set<ConstraintViolation<T>> doValidation(T bean) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> errors = validator.validate(bean);
return errors;
}
示例9: validate
import javax.validation.Validator; //导入方法依赖的package包/类
public static <T> void validate(T object) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> validatorSet = validator.validate(object);
validatorSet.forEach(cv -> {
throw new BeanValidationException(cv.getPropertyPath() + " - " + cv.getMessage());
});
}
示例10: assertViolation
import javax.validation.Validator; //导入方法依赖的package包/类
private static <T> void assertViolation(@Nullable final Class<? extends Annotation> clazz, final T bean, @Nullable final String property,
final boolean expectedMatching, final String messageFormat, Class... groups) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> violations = null;
if (groups == null || groups.length == 0) {
violations = validator.validate(bean);
} else {
violations = validator.validate(bean, groups);
}
ConstraintViolation<T> matchingViolation = null;
for (ConstraintViolation<T> violation : violations) {
if ((clazz == null || violation.getConstraintDescriptor().getAnnotation().annotationType().equals(clazz))
&& (property == null || violation.getPropertyPath().toString().equals(property))) {
matchingViolation = violation;
break;
}
}
if (expectedMatching && matchingViolation == null || !expectedMatching && matchingViolation != null) {
Class<? extends Annotation> annotation = clazz;
String prop = property;
String details = "";
if (matchingViolation != null) {
annotation = matchingViolation.getConstraintDescriptor().getAnnotation().annotationType();
prop = matchingViolation.getPropertyPath().toString();
details = " - " + matchingViolation;
}
throw new AssertionError(MessageFormat.format(messageFormat, annotation, prop) + details);
}
}
示例11: testDefaultMetadataBeanValidation
import javax.validation.Validator; //导入方法依赖的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());
}
}
示例12: validateTodoValid
import javax.validation.Validator; //导入方法依赖的package包/类
@Test
public void validateTodoValid() {
ToDo toDo = new ToDo("[email protected]","caption", "description 1", 6);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<ToDo>> constraintViolations = validator.validate(toDo);
assertTrue(constraintViolations.isEmpty());
}
示例13: validateTodoInValid
import javax.validation.Validator; //导入方法依赖的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());
}
}
}
示例14: checkParam
import javax.validation.Validator; //导入方法依赖的package包/类
public static <E> void checkParam(E obj, Class mafExceptionClass, Integer respCode, Class... validateGroup) throws Exception
{
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
Validator validator = vf.getValidator();
Set<ConstraintViolation<Object>> set = validator.validate(obj, validateGroup);
for(ConstraintViolation<Object> cv : set)
{
Constructor constructor = mafExceptionClass.getConstructor(int.class, String.class);
MafiaException mafException = (MafiaException) constructor.newInstance(respCode, cv.getMessage());
throw mafException;
}
}
示例15: validateIndividuBean
import javax.validation.Validator; //导入方法依赖的package包/类
/** Valide un bean d'individu
* @param ind
* @throws CustomException
*/
public void validateIndividuBean(Individu ind) throws CustomException{
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Individu>> constraintViolations = validator.validate(ind);
if (constraintViolations!=null && constraintViolations.size() > 0) {
String erreur = "";
for (ConstraintViolation<?> violation : constraintViolations) {
erreur += (" *** "+ violation.getPropertyPath().toString() + " : " + violation.getMessage());
}
throw new CustomException(applicationContext.getMessage("droitprofil.individu.error", null, UI.getCurrent().getLocale())+" : "+erreur);
}
}