Java Code Examples for javax.validation.ConstraintViolation#getMessage()
The following examples show how to use
javax.validation.ConstraintViolation#getMessage() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TagService.java From blog-spring with MIT License | 6 votes |
public Tag findOrCreateByName(String name) { Tag tag = tagRepository.findByName(name); if (tag == null) { try { tag = new Tag(); tag.setName(name); tag.setPostCount(0); tagRepository.save(tag); } catch (ConstraintViolationException exception) { ConstraintViolation<?> violation = exception.getConstraintViolations().iterator().next(); throw new InvalidTagException( "Invalid tag " + violation.getPropertyPath() + ": " + violation.getMessage()); } } return tag; }
Example 2
Source File: User.java From picocli with Apache License 2.0 | 6 votes |
private void validate() { System.out.println(spec.commandLine().getParseResult().originalArgs()); System.out.println(this); ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Set<ConstraintViolation<User>> violations = validator.validate(this); if (!violations.isEmpty()) { String errorMsg = ""; for (ConstraintViolation<User> violation : violations) { errorMsg += "ERROR: " + violation.getMessage() + "\n"; } throw new ParameterException(spec.commandLine(), errorMsg); } }
Example 3
Source File: HibernateValidatorProperty.java From pm-wicket-utils with GNU Lesser General Public License v3.0 | 6 votes |
public void validate(IValidatable<Object> validatable) { Validator validator = HibernateValidatorProperty.validatorFactory.getValidator(); @SuppressWarnings("unchecked") Set<ConstraintViolation<Object>> violations = validator.validateValue((Class<Object>)beanModel.getObject().getClass(), propertyName, validatable.getValue()); if(!violations.isEmpty()){ for(ConstraintViolation<?> violation : violations){ ValidationError error = new ValidationError(violation.getMessage()); String key = violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(); if(getValidatorPrefix()!=null) key = getValidatorPrefix()+"."+key; error.addKey(key); error.setVariables(new HashMap<String, Object>(violation.getConstraintDescriptor().getAttributes())); //remove garbage from the attributes error.getVariables().remove("payload"); error.getVariables().remove("message"); error.getVariables().remove("groups"); validatable.error(error); } } }
Example 4
Source File: RestViolationProcessor.java From osiris with Apache License 2.0 | 6 votes |
public void processMethodParameterValidation(Collection<ConstraintViolation<Object>> violations) throws Exception { String constraintMessage = ""; if (!violations.isEmpty()){ try { for (ConstraintViolation<Object> constraintViolation : violations) { if(!(constraintViolation.getInvalidValue() instanceof Collection)){ constraintMessage += constraintViolation.getMessage() + " Value:" +constraintViolation.getInvalidValue() + " ,"; }else{ constraintMessage += constraintViolation.getMessage() + " ,"; } } constraintMessage = constraintMessage.substring(0, constraintMessage.length() - 2); } catch (Throwable e) {// I don't trust it works in every case e.printStackTrace(); } throw new InvalidParametersException(constraintMessage); } }
Example 5
Source File: GlobalExceptionHandler.java From BigDataPlatform with GNU General Public License v3.0 | 5 votes |
@ExceptionHandler(ValidationException.class) @ResponseBody public Object badArgumentHandler(ValidationException e) { logger.error(e.getMessage(), e); if (e instanceof ConstraintViolationException) { ConstraintViolationException exs = (ConstraintViolationException) e; Set<ConstraintViolation<?>> violations = exs.getConstraintViolations(); for (ConstraintViolation<?> item : violations) { String message = ((PathImpl) item.getPropertyPath()).getLeafNode().getName() + item.getMessage(); return ResponseUtil.fail(402, message); } } return ResponseUtil.badArgumentValue(); }
Example 6
Source File: ProductValidator.java From maven-framework-project with MIT License | 5 votes |
public void validate(Object target, Errors errors) { Set<ConstraintViolation<Object>> constraintViolations = beanValidator.validate(target); for (ConstraintViolation<Object> constraintViolation : constraintViolations) { String propertyPath = constraintViolation.getPropertyPath().toString(); String message = constraintViolation.getMessage(); errors.rejectValue(propertyPath, "", message); } for(Validator validator: springValidators) { validator.validate(target, errors); } }
Example 7
Source File: ValidationByExceptionHandler.java From blog with MIT License | 5 votes |
<T> void validate(T object) { Set<ConstraintViolation<T>> errs = jeeValidator.validate(object); if (errs.size() > 0) { // error String msg = "Invalid Bean, constraint error(s) : "; for (ConstraintViolation<T> err : errs) { msg += err.getPropertyPath() + " " + err.getMessage() + ". "; } throw new IllegalArgumentException(msg); } }
Example 8
Source File: GlobalExceptionHandler.java From dts-shop with GNU Lesser General Public License v3.0 | 5 votes |
@ExceptionHandler(ValidationException.class) @ResponseBody public Object badArgumentHandler(ValidationException e) { e.printStackTrace(); if (e instanceof ConstraintViolationException) { ConstraintViolationException exs = (ConstraintViolationException) e; Set<ConstraintViolation<?>> violations = exs.getConstraintViolations(); for (ConstraintViolation<?> item : violations) { String message = ((PathImpl) item.getPropertyPath()).getLeafNode().getName() + item.getMessage(); return ResponseUtil.fail(402, message); } } return ResponseUtil.badArgumentValue(); }
Example 9
Source File: PaymentValidate.java From aaden-pay with Apache License 2.0 | 5 votes |
private void validCash(PayRequest request) throws Exception { Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); validator.validate(request.getCash()); Set<ConstraintViolation<PayCashData>> validators = validator.validate(request.getCash()); for (ConstraintViolation<PayCashData> constraintViolation : validators) { throw new Exception(constraintViolation.getMessage()); } }
Example 10
Source File: RestExceptionHandler.java From runscore with Apache License 2.0 | 5 votes |
@ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(value = HttpStatus.OK) public Result handleConstraintViolationException(ConstraintViolationException e) { String msg = "param valid exception"; if (e != null) { Iterator<ConstraintViolation<?>> iterator = e.getConstraintViolations().iterator(); if (iterator.hasNext()) { ConstraintViolation<?> violation = iterator.next(); msg = violation.getPropertyPath() + ":" + violation.getMessage(); } log.warn(e.toString()); } return Result.fail(msg); }
Example 11
Source File: ValidatorUtils.java From renren-fast with GNU General Public License v3.0 | 5 votes |
/** * 校验对象 * @param object 待校验对象 * @param groups 待校验的组 * @throws RRException 校验不通过,则报RRException异常 */ public static void validateEntity(Object object, Class<?>... groups) throws RRException { Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups); if (!constraintViolations.isEmpty()) { ConstraintViolation<Object> constraint = (ConstraintViolation<Object>)constraintViolations.iterator().next(); throw new RRException(constraint.getMessage()); } }
Example 12
Source File: ValidatorUtils.java From EasyEE with MIT License | 5 votes |
/** * 根据实体上的注解校验实体 * * @param t * @return */ public static <T> String validate(T t) { Set<ConstraintViolation<T>> constraintViolations = validator .validate(t); String validateError = ""; if (constraintViolations.size() > 0) { for (ConstraintViolation<T> constraintViolation : constraintViolations) { validateError = constraintViolation.getMessage(); break; } } return validateError; }
Example 13
Source File: DescriptorValidatorImpl.java From cm_ext with Apache License 2.0 | 5 votes |
@Override public Set<String> validate(T descriptor) { Set<ConstraintViolation<T>> constraintViolations; constraintViolations = getViolations(descriptor); ImmutableSet.Builder<String> violations = ImmutableSet.builder(); for (ConstraintViolation<T> violation : constraintViolations) { String message = violation.getMessage(); String relativePath = violation.getPropertyPath().toString(); violations.add(String.format(ERROR_FORMAT, errorPrefix, relativePath, message)); } return violations.build(); }
Example 14
Source File: ValidationExceptionMapper.java From jweb-cms with GNU Affero General Public License v3.0 | 5 votes |
@Override public Response toResponse(ValidationException exception) { ValidationExceptionResponse response = new ValidationExceptionResponse(); if (exception instanceof ConstraintViolationException) { ConstraintViolation<?> violation = ((ConstraintViolationException) exception).getConstraintViolations().iterator().next(); response.path = violation.getPropertyPath().toString(); response.errorMessage = violation.getMessage(); } else { response.errorMessage = exception.getMessage(); } return Response.status(400).entity(response) .type(MediaType.APPLICATION_JSON).build(); }
Example 15
Source File: GlobalExceptionHandler.java From mall with MIT License | 5 votes |
@ExceptionHandler(ValidationException.class) @ResponseBody public Object badArgumentHandler(ValidationException e) { e.printStackTrace(); if (e instanceof ConstraintViolationException) { ConstraintViolationException exs = (ConstraintViolationException) e; Set<ConstraintViolation<?>> violations = exs.getConstraintViolations(); for (ConstraintViolation<?> item : violations) { String message = ((PathImpl) item.getPropertyPath()).getLeafNode().getName() + item.getMessage(); return ResponseUtil.fail(402, message); } } return ResponseUtil.badArgumentValue(); }
Example 16
Source File: GlobalExceptionHandler.java From Spring-Boot-Book with Apache License 2.0 | 5 votes |
/** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(ConstraintViolationException.class) public String handleServiceException(ConstraintViolationException e, Model model) { logger.error("参数验证失败", e); Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); ConstraintViolation<?> violation = violations.iterator().next(); String message = "【参数验证失败】" + violation.getMessage(); model.addAttribute("message", message); model.addAttribute("code", 400); return viewName; }
Example 17
Source File: AbstractCmEntityProvider.java From sakai with Educational Community License v2.0 | 5 votes |
protected void validateDataObject(Object data) { if (!(data instanceof CmEntityData)) throw new IllegalArgumentException("Request body must implement CmEntityData interface."); Set<ConstraintViolation<Object>> constraintViolations = validator.validate(data); if (constraintViolations.isEmpty()) return; String errorMessage = "Invalid " + data.getClass().getSimpleName() + ":"; for (ConstraintViolation violation : constraintViolations) { errorMessage += "\n" + violation.getPropertyPath() + " " + violation.getMessage(); } throw new IllegalArgumentException(errorMessage); }
Example 18
Source File: GlobalExceptionHandler.java From short-url with Apache License 2.0 | 5 votes |
@ExceptionHandler @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public String handle(ConstraintViolationException exception) { List<ConstraintViolation<?>> constraintViolations = Lists.newArrayList(exception.getConstraintViolations()); ConstraintViolation<?> constraintViolation = constraintViolations.get(0); return "bad request, " + constraintViolation.getMessage(); }
Example 19
Source File: GlobalExceptionHandler.java From Spring-Boot-Book with Apache License 2.0 | 5 votes |
/** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(ConstraintViolationException.class) public Map<String, Object> handleServiceException(ConstraintViolationException e) { logger.error("参数验证失败", e); Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); ConstraintViolation<?> violation = violations.iterator().next(); String message = violation.getMessage(); Map<String, Object> map = new HashMap<String, Object>(); map.put("rspCode", 400); map.put("rspMsg", message); //发生异常进行日志记录,写入数据库或者其他处理,此处省略 return map; }
Example 20
Source File: ValidationUtil.java From BlogManagePlatform with Apache License 2.0 | 2 votes |
/** * 获取格式化的错误信息 * @author Frodez * @date 2019-06-11 */ private static String getErrorMessage(ConstraintViolation<Object> violation) { String errorSource = getErrorSource(violation); return errorSource == null ? violation.getMessage() : StrUtil.concat(errorSource, DefStr.SEPERATOR, violation.getMessage()); }