本文整理匯總了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);
}
}