本文整理汇总了Java中javax.validation.ConstraintViolation.getMessage方法的典型用法代码示例。如果您正苦于以下问题:Java ConstraintViolation.getMessage方法的具体用法?Java ConstraintViolation.getMessage怎么用?Java ConstraintViolation.getMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.validation.ConstraintViolation
的用法示例。
在下文中一共展示了ConstraintViolation.getMessage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validate
import javax.validation.ConstraintViolation; //导入方法依赖的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);
}
}
示例2: handleConstraintViolation
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
private void handleConstraintViolation(ConstraintViolationException violationException){
Set<ConstraintViolation<?>> violationSet = violationException.getConstraintViolations();
String[] messages = new String[violationSet.size()];
int i = 0;
for(ConstraintViolation violation: violationSet){
messages[i] = violation.getMessage();
i++;
}
Gson gson = appContext.getGsonBuilder().create();
Map details = new HashMap<>();
details.put("messages",messages);
Map exception = new HashMap<>();
exception.put("type",ConstraintViolationException.class.getSimpleName());
exception.put("details",details);
String resp = gson.toJson(exception);
requestContext.getResponse().setStatus(500);
requestContext.getResponse().setContentType("text/json;charset=UTF8");
requestContext.echo(resp);
}
示例3: verifyMethod
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
/**
* Validate the method parameters based on javax.validation: Hibernate Validator.
*
* @param proxy The checked target object.
* @param method The checked target method.
* @param args The checked target method's parameters.
* @param <T> The target object type: Generic types.
*/
public <T> void verifyMethod(
final T proxy,
final Method method,
final Object[] args) {
// 1. Get method validator
final ExecutableValidator validatorParam
= VALIDATOR.forExecutables();
// 2. Create new params that wait for validation
final Set<ConstraintViolation<T>> constraints
= validatorParam.validateParameters(proxy, method, args);
// 3. Throw out exception
if (!constraints.isEmpty()) {
final ConstraintViolation<T> item = constraints.iterator().next();
if (null != item) {
final WebException error
= new _400ValidationException(getClass(),
proxy.getClass(), method, item.getMessage());
error.setReadible(item.getMessage());
throw error;
}
}
}
示例4: prepareMessage
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
private static String prepareMessage(ConstraintViolation property) {
if (!StringUtils.isEmpty(property.getPropertyPath().toString())) {
return property.getPropertyPath() + " : " + property.getMessage();
}
return property.getMessage();
}
示例5: JSR303Validator
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
/**
* 使用hibernate-validator实现的JSR303验证model
*/
public <T> void JSR303Validator(T model) {
Set<ConstraintViolation<T>> constraintViolations = validator.validate(model);
if(constraintViolations.size() == 0) return;
for(ConstraintViolation<T> c : constraintViolations) {
throw new AssertException("Oop~ " + c.getPropertyPath() + " " + c.getMessage());
}
}
示例6: validateEntity
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
/**
* 校验对象
* @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());
}
}
示例7: fromConstraintViolation
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
public static Violation fromConstraintViolation(final ConstraintViolation<?> constraintViolation) {
final String property = constraintViolation.getPropertyPath().toString();
final ConstraintDescriptor<?> constraintDescriptor = constraintViolation.getConstraintDescriptor();
final Annotation annotation = constraintDescriptor.getAnnotation();
final Class<? extends Annotation> annotationType = annotation.annotationType();
final String error = annotationType.getSimpleName();
final String message = constraintViolation.getMessage();
return new Builder().property(property).error(error).message(message).build();
}
示例8: check
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
/**
* 检查参数是否符合注解(用户自行加的验证注解)
*
* @param params 参数
* @throws ValidationException 当参数不符合规定时抛出该异常
*/
public void check(Object[] params) throws ValidationException {
Set<ConstraintViolation<T>> methodValidators = executableValidator.validateParameters(instance,
resourceMethod, params);
for (ConstraintViolation<T> constraintViolation : methodValidators) {
throw new ValidationException(constraintViolation.getMessage());
}
}
示例9: handle
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
@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();
}
示例10: validMust
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
private void validMust(PayRequest request) throws Exception {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
validator.validate(request.getMust());
Set<ConstraintViolation<PayMustData>> validators = validator.validate(request.getMust());
for (ConstraintViolation<PayMustData> constraintViolation : validators) {
throw new Exception(constraintViolation.getMessage());
}
}
示例11: validCash
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
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());
}
}
示例12: validRecharge
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
private void validRecharge(PayRequest request) throws Exception {
if (request.getSys().getPayType() == PayType.GATEWAY)
return;
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
validator.validate(request.getRecharge());
Set<ConstraintViolation<PayRechargeData>> validators = validator.validate(request.getRecharge());
for (ConstraintViolation<PayRechargeData> constraintViolation : validators) {
throw new Exception(constraintViolation.getMessage());
}
}
示例13: handleServiceException
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public Response handleServiceException(ConstraintViolationException e) {
log.error("参数验证失败: {}", e);
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
ConstraintViolation<?> violation = violations.iterator().next();
String message = violation.getMessage();
return new Response().failure("parameter:" + message);
}
示例14: validateEntityFields
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
public static <T> void validateEntityFields(Validator validator, T entity) {
Set<ConstraintViolation<T>> errors = validator.validate(entity);
Iterator<ConstraintViolation<T>> itErrors = errors.iterator();
if (itErrors.hasNext()) {
ConstraintViolation<T> violation = itErrors.next();
throw new FieldNotValidException(violation.getPropertyPath().toString(), violation.getMessage());
}
}
示例15: validate
import javax.validation.ConstraintViolation; //导入方法依赖的package包/类
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);
}
}